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
10 namespace Zend\Captcha
;
12 use DirectoryIterator
;
13 use Zend\Captcha\Exception
;
14 use Zend\Stdlib\ErrorHandler
;
17 * Image-based captcha element
19 * Generates image displaying random word
21 class Image
extends AbstractWord
24 * Directory for generated images
28 protected $imgDir = "public/images/captcha/";
31 * URL for accessing images
35 protected $imgUrl = "/images/captcha/";
38 * Image's alt tag content
42 protected $imgAlt = "";
45 * Image suffix (including dot)
49 protected $suffix = ".png";
56 protected $width = 200;
63 protected $height = 50;
70 protected $fsize = 24;
80 * Image to use as starting point
81 * Default is blank image. If provided, should be PNG image.
85 protected $startImage;
88 * How frequently to execute garbage collection
92 protected $gcFreq = 10;
95 * How long to keep generated images
99 protected $expiration = 600;
102 * Number of noise dots on image
103 * Used twice - before and after transform
107 protected $dotNoiseLevel = 100;
110 * Number of noise lines on image
111 * Used twice - before and after transform
115 protected $lineNoiseLevel = 5;
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);
143 public function getImgAlt()
145 return $this->imgAlt
;
151 public function getStartImage()
153 return $this->startImage
;
159 public function getDotNoiseLevel()
161 return $this->dotNoiseLevel
;
167 public function getLineNoiseLevel()
169 return $this->lineNoiseLevel
;
173 * Get captcha expiration
177 public function getExpiration()
179 return $this->expiration
;
183 * Get garbage collection frequency
187 public function getGcFreq()
189 return $this->gcFreq
;
193 * Get font to use when generating captcha
197 public function getFont()
207 public function getFontSize()
213 * Get captcha image height
217 public function getHeight()
219 return $this->height
;
223 * Get captcha image directory
227 public function getImgDir()
229 return $this->imgDir
;
233 * Get captcha image base URL
237 public function getImgUrl()
239 return $this->imgUrl
;
243 * Get captcha image file suffix
247 public function getSuffix()
249 return $this->suffix
;
253 * Get captcha image width
257 public function getWidth()
263 * @param string $startImage
266 public function setStartImage($startImage)
268 $this->startImage
= $startImage;
273 * @param int $dotNoiseLevel
276 public function setDotNoiseLevel($dotNoiseLevel)
278 $this->dotNoiseLevel
= $dotNoiseLevel;
283 * @param int $lineNoiseLevel
286 public function setLineNoiseLevel($lineNoiseLevel)
288 $this->lineNoiseLevel
= $lineNoiseLevel;
293 * Set captcha expiration
295 * @param int $expiration
298 public function setExpiration($expiration)
300 $this->expiration
= $expiration;
305 * Set garbage collection frequency
310 public function setGcFreq($gcFreq)
312 $this->gcFreq
= $gcFreq;
319 * @param string $font
322 public function setFont($font)
329 * Set captcha font size
334 public function setFontSize($fsize)
336 $this->fsize
= $fsize;
341 * Set captcha image height
346 public function setHeight($height)
348 $this->height
= $height;
353 * Set captcha image storage directory
355 * @param string $imgDir
358 public function setImgDir($imgDir)
360 $this->imgDir
= rtrim($imgDir, "/\\") . '/';
365 * Set captcha image base URL
367 * @param string $imgUrl
370 public function setImgUrl($imgUrl)
372 $this->imgUrl
= rtrim($imgUrl, "/\\") . '/';
377 * @param string $imgAlt
380 public function setImgAlt($imgAlt)
382 $this->imgAlt
= $imgAlt;
387 * Set captcha image filename suffix
389 * @param string $suffix
392 public function setSuffix($suffix)
394 $this->suffix
= $suffix;
399 * Set captcha image width
404 public function setWidth($width)
406 $this->width
= $width;
411 * Generate random frequency
415 protected function randomFreq()
417 return mt_rand(700000, 1000000) / 15000000;
421 * Generate random phase
425 protected function randomPhase()
427 // random phase from 0 to pi
428 return mt_rand(0, 3141592) / 1000000;
432 * Generate random character size
436 protected function randomSize()
438 return mt_rand(300, 700) / 100;
444 * @return string captcha ID
446 public function generate()
448 $id = parent
::generate();
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();
456 $this->generateImage($id, $this->getWord());
458 if (mt_rand(1, $this->getGcFreq()) == 1) {
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();
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);
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
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);
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);
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) {
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) {
558 } elseif ($color == 0 && $colorX == 0 && $colorY == 0 && $colorXY == 0) {
559 // transfer inside of the image as-is
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));
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);
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) {
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
623 public function getHelperName()
625 return 'captcha/image';