XOOPS  2.6.0
imageclass.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
28 {
32  public $config = array();
36  public $code;
37 
41  public $mode = 'gd';
42 
46  public $invalid = false;
47 
51  public $font;
52 
56  public $spacing;
57 
61  public $width;
62 
66  public $height;
67 
72 
76  public $oImage;
77 
81  protected $xoops_root_path;
82 
86  public function __construct()
87  {
88  $this->captcha_handler = XoopsCaptcha::getInstance();
89  $this->config = $this->captcha_handler->loadConfig("image");
90  $this->xoops_root_path = \XoopsBaseConfig::get('root-path');
91  }
92 
98  public function loadImage()
99  {
100  $this->generateCode();
101  $this->createImage();
102  }
103 
109  public function generateCode()
110  {
111  if ($this->invalid) {
112  return false;
113  }
114 
115  if ($this->mode == "bmp") {
116  $this->config["num_chars"] = 4;
117  $this->code = rand(pow(10, $this->config["num_chars"] - 1), intval(str_pad("9", $this->config["num_chars"], "9")));
118  } else {
119  $raw_code = md5(uniqid(mt_rand(), 1));
120  if (!empty($this->config["skip_characters"])) {
121  $valid_code = str_replace($this->config["skip_characters"], "", $raw_code);
122  $this->code = substr($valid_code, 0, $this->config["num_chars"]);
123  } else {
124  $this->code = substr($raw_code, 0, $this->config["num_chars"]);
125  }
126  if (!$this->config["casesensitive"]) {
127  $this->code = strtoupper($this->code);
128  }
129  }
130  $this->captcha_handler->setCode($this->code);
131  return true;
132  }
133 
139  public function createImage()
140  {
141  if ($this->invalid) {
142  header("Content-type: image/gif");
143  readfile($this->xoops_root_path . "/images/subject/icon2.gif");
144  return false;
145  }
146 
147  if ($this->mode == "bmp") {
148  return $this->createImageBmp();
149  } else {
150  return $this->createImageGd();
151  }
152  }
153 
162  public function getList($name, $extension = "")
163  {
164  if ($items = Xoops_Cache::read("captcha_captcha_{$name}")) {
165  return $items;
166  }
167  ;
168  $file_path = $this->xoops_root_path . "/class/captcha/image/{$name}";
169  $files = XoopsLists::getFileListAsArray($file_path);
170  foreach ($files as $item) {
171  if (empty($extension) || preg_match("/(\.{$extension})$/i", $item)) {
172  $items[] = $item;
173  }
174  }
175  Xoops_Cache::write("captcha_captcha_{$name}", $items);
176  return $items;
177  }
178 
191  public function createImageGd()
192  {
193  $this->loadFont();
194  $this->setImageSize();
195 
196  $this->oImage = imagecreatetruecolor($this->width, $this->height);
197  $background = imagecolorallocate($this->oImage, 255, 255, 255);
198  imagefilledrectangle($this->oImage, 0, 0, $this->width, $this->height, $background);
199  switch ($this->config["background_type"]) {
200  case 0:
201  $this->drawBars();
202  break;
203  case 1:
204  $this->drawCircles();
205  break;
206  case 2:
207  $this->drawLines();
208  break;
209  case 3:
210  $this->drawRectangles();
211  break;
212  case 4:
213  $this->drawEllipses();
214  break;
215  case 5:
216  $this->drawPolygons();
217  break;
218  case 100:
219  $this->createFromFile();
220  break;
221  default:
222  }
223  $this->drawBorder();
224  $this->drawCode();
225 
226  header("Content-type: image/jpeg");
227  imagejpeg($this->oImage);
228  imagedestroy($this->oImage);
229  }
230 
236  public function loadFont()
237  {
238  $fonts = $this->getList("fonts", "ttf");
239  $this->font = $this->xoops_root_path . "/class/captcha/image/fonts/" . $fonts[array_rand($fonts)];
240  }
241 
247  public function setImageSize()
248  {
249  if (empty($this->font)) $this->loadFont();
250  $MaxCharWidth = 0;
251  $MaxCharHeight = 0;
252  $oImage = imagecreatetruecolor(100, 100);
253  $FontSize = $this->config["fontsize_max"];
254  for ($Angle = -30; $Angle <= 30; ++$Angle) {
255  for ($i = 65; $i <= 90; ++$i) {
256  $CharDetails = imageftbbox($FontSize, $Angle, $this->font, chr($i), array());
257  $_MaxCharWidth = abs($CharDetails[0] + $CharDetails[2]);
258  if ($_MaxCharWidth > $MaxCharWidth) {
259  $MaxCharWidth = $_MaxCharWidth;
260  }
261  $_MaxCharHeight = abs($CharDetails[1] + $CharDetails[5]);
262  if ($_MaxCharHeight > $MaxCharHeight) {
263  $MaxCharHeight = $_MaxCharHeight;
264  }
265  }
266  }
267  imagedestroy($oImage);
268 
269  $this->height = $MaxCharHeight + 2;
270  $this->spacing = intval(($this->config["num_chars"] * $MaxCharWidth) / $this->config["num_chars"]);
271  $this->width = intval(($this->config["num_chars"] * $MaxCharWidth) + ($this->spacing / 2));
272  }
273 
279  public function loadBackground()
280  {
281  $RandBackground = null;
282  if ($backgrounds = $this->getList("backgrounds", "(gif|jpg|png)")) {
283  $RandBackground = $this->xoops_root_path . "/class/captcha/image/backgrounds/" . $backgrounds[array_rand($backgrounds)];
284  }
285  return $RandBackground;
286  }
287 
293  public function createFromFile()
294  {
295  if ($RandImage = $this->loadBackground()) {
296  $ImageType = @getimagesize($RandImage);
297  switch (@$ImageType[2]) {
298  case 1:
299  $BackgroundImage = imagecreatefromgif($RandImage);
300  break;
301  case 2:
302  $BackgroundImage = imagecreatefromjpeg($RandImage);
303  break;
304  case 3:
305  $BackgroundImage = imagecreatefrompng($RandImage);
306  break;
307  }
308  }
309  if (isset($BackgroundImage) && !empty($BackgroundImage)) {
310  imagecopyresized($this->oImage, $BackgroundImage, 0, 0, 0, 0, imagesx($this->oImage), imagesy($this->oImage), imagesx($BackgroundImage), imagesy($BackgroundImage));
311  imagedestroy($BackgroundImage);
312  } else {
313  $this->drawBars();
314  }
315  }
316 
322  public function drawCode()
323  {
324  for ($i = 0; $i < $this->config["num_chars"]; ++$i) {
325  // select random greyscale colour
326  $text_color = imagecolorallocate($this->oImage, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
327 
328  // write text to image
329  $Angle = mt_rand(10, 30);
330  if (($i % 2)) {
331  $Angle = mt_rand(-30, -10);
332  }
333 
334  // select random font size
335  $FontSize = mt_rand($this->config["fontsize_min"], $this->config["fontsize_max"]);
336 
337  $CharDetails = imageftbbox($FontSize, $Angle, $this->font, $this->code[$i], array());
338  $CharHeight = abs($CharDetails[1] + $CharDetails[5]);
339 
340  // calculate character starting coordinates
341  $posX = ($this->spacing / 2) + ($i * $this->spacing);
342  $posY = 2 + ($this->height / 2) + ($CharHeight / 4);
343 
344  imagefttext(
345  $this->oImage, $FontSize, $Angle, $posX, $posY, $text_color, $this->font, $this->code[$i], array()
346  );
347  }
348  }
349 
355  public function drawBorder()
356  {
357  $rgb = rand(50, 150);
358  $border_color = imagecolorallocate($this->oImage, $rgb, $rgb, $rgb);
359  imagerectangle($this->oImage, 0, 0, $this->width - 1, $this->height - 1, $border_color);
360  }
361 
367  public function drawCircles()
368  {
369  for ($i = 1; $i <= $this->config["background_num"]; ++$i) {
370  $randomcolor = imagecolorallocate($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
371  imagefilledellipse($this->oImage, mt_rand(0, $this->width - 10), mt_rand(0, $this->height - 3), mt_rand(10, 20), mt_rand(20, 30), $randomcolor);
372  }
373  }
374 
380  public function drawLines()
381  {
382  for ($i = 0; $i < $this->config["background_num"]; ++$i) {
383  $randomcolor = imagecolorallocate($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
384  imageline($this->oImage, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $randomcolor);
385  }
386  }
387 
393  public function drawRectangles()
394  {
395  for ($i = 1; $i <= $this->config["background_num"]; ++$i) {
396  $randomcolor = imagecolorallocate($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
397  imagefilledrectangle($this->oImage, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $randomcolor);
398  }
399  }
400 
406  public function drawBars()
407  {
408  for ($i = 0; $i <= $this->height;) {
409  $randomcolor = imagecolorallocate($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
410  imageline($this->oImage, 0, $i, $this->width, $i, $randomcolor);
411  $i = $i + 2.5;
412  }
413  for ($i = 0; $i <= $this->width;) {
414  $randomcolor = imagecolorallocate($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
415  imageline($this->oImage, $i, 0, $i, $this->height, $randomcolor);
416  $i = $i + 2.5;
417  }
418  }
419 
425  public function drawEllipses()
426  {
427  for ($i = 1; $i <= $this->config["background_num"]; ++$i) {
428  $randomcolor = imagecolorallocate($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
429  imageellipse($this->oImage, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $randomcolor);
430  }
431  }
432 
438  public function drawPolygons()
439  {
440  for ($i = 1; $i <= $this->config["background_num"]; ++$i) {
441  $randomcolor = imagecolorallocate($this->oImage, mt_rand(190, 255), mt_rand(190, 255), mt_rand(190, 255));
442  $coords = array();
443  for ($j = 1; $j <= $this->config["polygon_point"]; ++$j) {
444  $coords[] = mt_rand(0, $this->width);
445  $coords[] = mt_rand(0, $this->height);
446  }
447  imagefilledpolygon($this->oImage, $coords, $this->config["polygon_point"], $randomcolor);
448  }
449  }
450 
460  public function createImageBmp($file = "")
461  {
462  $image = "";
463 
464  if (empty($file)) {
465  header("Content-type: image/bmp");
466  echo $image;
467  exit();
468  } else {
469  return $image;
470  }
471  }
472 }
if(empty($image_id)) $image
Definition: image.php:37
static getInstance()
$i
Definition: dialog.php:68
if(!isset($xoops->paths[$path_type])) if($path_type== 'var') $file
Definition: browse.php:55
getList($name, $extension="")
Definition: imageclass.php:162
$files
Definition: index.php:35
exit
Definition: browse.php:104
static read($key)
Definition: Legacy.php:94
static get($name)
static write($key, $value, $duration=0)
Definition: Legacy.php:76
$j
Definition: help.php:169