Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Captcha / Image.php
blob68a6e963a02fa1d5101145aa4eaeafd72d558ae6
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Captcha;
12 use DirectoryIterator;
13 use Zend\Captcha\Exception;
14 use Zend\Stdlib\ErrorHandler;
16 /**
17 * Image-based captcha element
19 * Generates image displaying random word
21 class Image extends AbstractWord
23 /**
24 * Directory for generated images
26 * @var string
28 protected $imgDir = "public/images/captcha/";
30 /**
31 * URL for accessing images
33 * @var string
35 protected $imgUrl = "/images/captcha/";
37 /**
38 * Image's alt tag content
40 * @var string
42 protected $imgAlt = "";
44 /**
45 * Image suffix (including dot)
47 * @var string
49 protected $suffix = ".png";
51 /**
52 * Image width
54 * @var int
56 protected $width = 200;
58 /**
59 * Image height
61 * @var int
63 protected $height = 50;
65 /**
66 * Font size
68 * @var int
70 protected $fsize = 24;
72 /**
73 * Image font file
75 * @var string
77 protected $font;
79 /**
80 * Image to use as starting point
81 * Default is blank image. If provided, should be PNG image.
83 * @var string
85 protected $startImage;
87 /**
88 * How frequently to execute garbage collection
90 * @var int
92 protected $gcFreq = 10;
94 /**
95 * How long to keep generated images
97 * @var int
99 protected $expiration = 600;
102 * Number of noise dots on image
103 * Used twice - before and after transform
105 * @var int
107 protected $dotNoiseLevel = 100;
110 * Number of noise lines on image
111 * Used twice - before and after transform
113 * @var int
115 protected $lineNoiseLevel = 5;
118 * Constructor
120 * @param array|\Traversable $options
121 * @throws Exception\ExtensionNotLoadedException
123 public function __construct($options = null)
125 if (!extension_loaded("gd")) {
126 throw new Exception\ExtensionNotLoadedException("Image CAPTCHA requires GD extension");
129 if (!function_exists("imagepng")) {
130 throw new Exception\ExtensionNotLoadedException("Image CAPTCHA requires PNG support");
133 if (!function_exists("imageftbbox")) {
134 throw new Exception\ExtensionNotLoadedException("Image CAPTCHA requires FT fonts support");
137 parent::__construct($options);
141 * @return string
143 public function getImgAlt()
145 return $this->imgAlt;
149 * @return string
151 public function getStartImage()
153 return $this->startImage;
157 * @return int
159 public function getDotNoiseLevel()
161 return $this->dotNoiseLevel;
165 * @return int
167 public function getLineNoiseLevel()
169 return $this->lineNoiseLevel;
173 * Get captcha expiration
175 * @return int
177 public function getExpiration()
179 return $this->expiration;
183 * Get garbage collection frequency
185 * @return int
187 public function getGcFreq()
189 return $this->gcFreq;
193 * Get font to use when generating captcha
195 * @return string
197 public function getFont()
199 return $this->font;
203 * Get font size
205 * @return int
207 public function getFontSize()
209 return $this->fsize;
213 * Get captcha image height
215 * @return int
217 public function getHeight()
219 return $this->height;
223 * Get captcha image directory
225 * @return string
227 public function getImgDir()
229 return $this->imgDir;
233 * Get captcha image base URL
235 * @return string
237 public function getImgUrl()
239 return $this->imgUrl;
243 * Get captcha image file suffix
245 * @return string
247 public function getSuffix()
249 return $this->suffix;
253 * Get captcha image width
255 * @return int
257 public function getWidth()
259 return $this->width;
263 * @param string $startImage
264 * @return Image
266 public function setStartImage($startImage)
268 $this->startImage = $startImage;
269 return $this;
273 * @param int $dotNoiseLevel
274 * @return Image
276 public function setDotNoiseLevel($dotNoiseLevel)
278 $this->dotNoiseLevel = $dotNoiseLevel;
279 return $this;
283 * @param int $lineNoiseLevel
284 * @return Image
286 public function setLineNoiseLevel($lineNoiseLevel)
288 $this->lineNoiseLevel = $lineNoiseLevel;
289 return $this;
293 * Set captcha expiration
295 * @param int $expiration
296 * @return Image
298 public function setExpiration($expiration)
300 $this->expiration = $expiration;
301 return $this;
305 * Set garbage collection frequency
307 * @param int $gcFreq
308 * @return Image
310 public function setGcFreq($gcFreq)
312 $this->gcFreq = $gcFreq;
313 return $this;
317 * Set captcha font
319 * @param string $font
320 * @return Image
322 public function setFont($font)
324 $this->font = $font;
325 return $this;
329 * Set captcha font size
331 * @param int $fsize
332 * @return Image
334 public function setFontSize($fsize)
336 $this->fsize = $fsize;
337 return $this;
341 * Set captcha image height
343 * @param int $height
344 * @return Image
346 public function setHeight($height)
348 $this->height = $height;
349 return $this;
353 * Set captcha image storage directory
355 * @param string $imgDir
356 * @return Image
358 public function setImgDir($imgDir)
360 $this->imgDir = rtrim($imgDir, "/\\") . '/';
361 return $this;
365 * Set captcha image base URL
367 * @param string $imgUrl
368 * @return Image
370 public function setImgUrl($imgUrl)
372 $this->imgUrl = rtrim($imgUrl, "/\\") . '/';
373 return $this;
377 * @param string $imgAlt
378 * @return Image
380 public function setImgAlt($imgAlt)
382 $this->imgAlt = $imgAlt;
383 return $this;
387 * Set captcha image filename suffix
389 * @param string $suffix
390 * @return Image
392 public function setSuffix($suffix)
394 $this->suffix = $suffix;
395 return $this;
399 * Set captcha image width
401 * @param int $width
402 * @return Image
404 public function setWidth($width)
406 $this->width = $width;
407 return $this;
411 * Generate random frequency
413 * @return float
415 protected function randomFreq()
417 return mt_rand(700000, 1000000) / 15000000;
421 * Generate random phase
423 * @return float
425 protected function randomPhase()
427 // random phase from 0 to pi
428 return mt_rand(0, 3141592) / 1000000;
432 * Generate random character size
434 * @return int
436 protected function randomSize()
438 return mt_rand(300, 700) / 100;
442 * Generate captcha
444 * @return string captcha ID
446 public function generate()
448 $id = parent::generate();
449 $tries = 5;
451 // If there's already such file, try creating a new ID
452 while ($tries-- && file_exists($this->getImgDir() . $id . $this->getSuffix())) {
453 $id = $this->generateRandomId();
454 $this->setId($id);
456 $this->generateImage($id, $this->getWord());
458 if (mt_rand(1, $this->getGcFreq()) == 1) {
459 $this->gc();
462 return $id;
466 * Generate image captcha
468 * Override this function if you want different image generator
469 * Wave transform from http://www.captcha.ru/captchas/multiwave/
471 * @param string $id Captcha ID
472 * @param string $word Captcha word
473 * @throws Exception\NoFontProvidedException if no font was set
474 * @throws Exception\ImageNotLoadableException if start image cannot be loaded
476 protected function generateImage($id, $word)
478 $font = $this->getFont();
480 if (empty($font)) {
481 throw new Exception\NoFontProvidedException('Image CAPTCHA requires font');
484 $w = $this->getWidth();
485 $h = $this->getHeight();
486 $fsize = $this->getFontSize();
488 $imgFile = $this->getImgDir() . $id . $this->getSuffix();
490 if (empty($this->startImage)) {
491 $img = imagecreatetruecolor($w, $h);
492 } else {
493 // Potential error is change to exception
494 ErrorHandler::start();
495 $img = imagecreatefrompng($this->startImage);
496 $error = ErrorHandler::stop();
497 if (!$img || $error) {
498 throw new Exception\ImageNotLoadableException(
499 "Can not load start image '{$this->startImage}'", 0, $error
502 $w = imagesx($img);
503 $h = imagesy($img);
506 $textColor = imagecolorallocate($img, 0, 0, 0);
507 $bgColor = imagecolorallocate($img, 255, 255, 255);
508 imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bgColor);
509 $textbox = imageftbbox($fsize, 0, $font, $word);
510 $x = ($w - ($textbox[2] - $textbox[0])) / 2;
511 $y = ($h - ($textbox[7] - $textbox[1])) / 2;
512 imagefttext($img, $fsize, 0, $x, $y, $textColor, $font, $word);
514 // generate noise
515 for ($i=0; $i < $this->dotNoiseLevel; $i++) {
516 imagefilledellipse($img, mt_rand(0, $w), mt_rand(0, $h), 2, 2, $textColor);
518 for ($i=0; $i < $this->lineNoiseLevel; $i++) {
519 imageline($img, mt_rand(0, $w), mt_rand(0, $h), mt_rand(0, $w), mt_rand(0, $h), $textColor);
522 // transformed image
523 $img2 = imagecreatetruecolor($w, $h);
524 $bgColor = imagecolorallocate($img2, 255, 255, 255);
525 imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bgColor);
527 // apply wave transforms
528 $freq1 = $this->randomFreq();
529 $freq2 = $this->randomFreq();
530 $freq3 = $this->randomFreq();
531 $freq4 = $this->randomFreq();
533 $ph1 = $this->randomPhase();
534 $ph2 = $this->randomPhase();
535 $ph3 = $this->randomPhase();
536 $ph4 = $this->randomPhase();
538 $szx = $this->randomSize();
539 $szy = $this->randomSize();
541 for ($x = 0; $x < $w; $x++) {
542 for ($y = 0; $y < $h; $y++) {
543 $sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3)) * $szx;
544 $sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4)) * $szy;
546 if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {
547 continue;
548 } else {
549 $color = (imagecolorat($img, $sx, $sy) >> 16) & 0xFF;
550 $colorX = (imagecolorat($img, $sx + 1, $sy) >> 16) & 0xFF;
551 $colorY = (imagecolorat($img, $sx, $sy + 1) >> 16) & 0xFF;
552 $colorXY = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF;
555 if ($color == 255 && $colorX == 255 && $colorY == 255 && $colorXY == 255) {
556 // ignore background
557 continue;
558 } elseif ($color == 0 && $colorX == 0 && $colorY == 0 && $colorXY == 0) {
559 // transfer inside of the image as-is
560 $newcolor = 0;
561 } else {
562 // do antialiasing for border items
563 $fracX = $sx - floor($sx);
564 $fracY = $sy - floor($sy);
565 $fracX1 = 1 - $fracX;
566 $fracY1 = 1 - $fracY;
568 $newcolor = $color * $fracX1 * $fracY1
569 + $colorX * $fracX * $fracY1
570 + $colorY * $fracX1 * $fracY
571 + $colorXY * $fracX * $fracY;
574 imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor));
578 // generate noise
579 for ($i=0; $i<$this->dotNoiseLevel; $i++) {
580 imagefilledellipse($img2, mt_rand(0, $w), mt_rand(0, $h), 2, 2, $textColor);
583 for ($i=0; $i<$this->lineNoiseLevel; $i++) {
584 imageline($img2, mt_rand(0, $w), mt_rand(0, $h), mt_rand(0, $w), mt_rand(0, $h), $textColor);
587 imagepng($img2, $imgFile);
588 imagedestroy($img);
589 imagedestroy($img2);
593 * Remove old files from image directory
596 protected function gc()
598 $expire = time() - $this->getExpiration();
599 $imgdir = $this->getImgDir();
600 if (!$imgdir || strlen($imgdir) < 2) {
601 // safety guard
602 return;
605 $suffixLength = strlen($this->suffix);
606 foreach (new DirectoryIterator($imgdir) as $file) {
607 if (!$file->isDot() && !$file->isDir()) {
608 if (file_exists($file->getPathname()) && $file->getMTime() < $expire) {
609 // only deletes files ending with $this->suffix
610 if (substr($file->getFilename(), -($suffixLength)) == $this->suffix) {
611 unlink($file->getPathname());
619 * Get helper name used to render captcha
621 * @return string
623 public function getHelperName()
625 return 'captcha/image';