PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/library/Dnez/Captcha/Image.php

https://bitbucket.org/vladimiraleksiev/mhbg
PHP | 208 lines | 143 code | 38 blank | 27 comment | 34 complexity | 3e92914ba789f1a337c4b772d0cda444 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. class Dnez_Captcha_Image extends Zend_Captcha_Image
  3. {
  4. private $_transparent = false;
  5. private $_textColor = array('r'=>0, 'g'=>0, 'b'=> 0);
  6. private $_backGroundColor = array('r'=>0, 'g'=>0, 'b'=> 0);
  7. public function setTransparent($transparent)
  8. {
  9. $this->_transparent = $transparent;
  10. return $this;
  11. }
  12. public function setTextColor ($color = array('r'=>0, 'g'=>0, 'b'=> 0))
  13. {
  14. $this->_textColor = $color;
  15. return $this;
  16. }
  17. public function getTextColor ()
  18. {
  19. return $this->_textColor;
  20. }
  21. public function getTransparent()
  22. {
  23. return $this->_transparent;
  24. }
  25. public function setBackgroundColor ($color = array('r'=>0, 'g'=>0, 'b'=> 0))
  26. {
  27. $this->_backGroundColor = $color;
  28. return $this;
  29. }
  30. public function getBackgroundColor ()
  31. {
  32. return $this->_backGroundColor;
  33. }
  34. protected function _generateImage($id, $word)
  35. {
  36. // $word = strtoupper($word);
  37. $this->_dotNoiseLevel = 8;
  38. $this->_lineNoiseLevel = 1;
  39. if (!extension_loaded("gd")) {
  40. require_once 'Zend/Captcha/Exception.php';
  41. throw new Zend_Captcha_Exception("Image CAPTCHA requires GD extension");
  42. }
  43. if (!function_exists("imagepng")) {
  44. require_once 'Zend/Captcha/Exception.php';
  45. throw new Zend_Captcha_Exception("Image CAPTCHA requires PNG support");
  46. }
  47. if (!function_exists("imageftbbox")) {
  48. require_once 'Zend/Captcha/Exception.php';
  49. throw new Zend_Captcha_Exception("Image CAPTCHA requires FT fonts support");
  50. }
  51. $font = $this->getFont();
  52. if (empty($font)) {
  53. require_once 'Zend/Captcha/Exception.php';
  54. throw new Zend_Captcha_Exception("Image CAPTCHA requires font");
  55. }
  56. $w = $this->getWidth();
  57. $h = $this->getHeight();
  58. $fsize = $this->getFontSize();
  59. $img_file = $this->getImgDir() . $id . $this->getSuffix();
  60. if(empty($this->_startImage)) {
  61. $img = imagecreatetruecolor($w, $h);
  62. } else {
  63. $img = imagecreatefrompng($this->_startImage);
  64. if(!$img) {
  65. require_once 'Zend/Captcha/Exception.php';
  66. throw new Zend_Captcha_Exception("Can not load start image");
  67. }
  68. $w = imagesx($img);
  69. $h = imagesy($img);
  70. }
  71. $text_color = imagecolorallocate($img, 0, 0, 0);
  72. $bg_color = imagecolorallocate($img, 255, 255, 255);
  73. imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color);
  74. $textbox = imageftbbox($fsize, 0, $font, $word);
  75. $x = ($w - ($textbox[2] - $textbox[0])) / 2;
  76. $y = ($h - ($textbox[7] - $textbox[1])) / 2;
  77. imagefttext($img, $fsize, 0, $x, $y, $text_color, $font, $word);
  78. // generate noise
  79. // for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
  80. // imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
  81. // }
  82. // for($i=0; $i<$this->_lineNoiseLevel; $i++) {
  83. // imageline($img, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
  84. // }
  85. // transformed image
  86. $img2 = imagecreatetruecolor($w, $h);
  87. //build image depending on transparency
  88. if($this->_transparent) {
  89. imagealphablending($img2, false);
  90. // $bg_color = imagecolorallocatealpha($img2,255,255,255,127);
  91. $bg_color = imagecolorallocatealpha($img2, $this->_backGroundColor['r'],$this->_backGroundColor['g'],$this->_backGroundColor['b'],127);//242,202,239,127);
  92. imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);
  93. imagealphablending($img2,true);
  94. } else {
  95. $img2 = imagecreatetruecolor($w, $h);
  96. // $bg_color = imagecolorallocate($img2, 255, 255, 255);
  97. // $bg_color = imagecolorallocate($img2, 242,204,239);
  98. $bg_color = imagecolorallocatealpha($img2, $this->_backGroundColor['r'],$this->_backGroundColor['g'],$this->_backGroundColor['b'], 0);
  99. imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);
  100. }
  101. // $bg_color = imagecolorallocate($img2, 255, 255, 255);
  102. // imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);
  103. // apply wave transforms
  104. $freq1 = $this->_randomFreq();
  105. $freq2 = $this->_randomFreq();
  106. $freq3 = $this->_randomFreq();
  107. $freq4 = $this->_randomFreq();
  108. $ph1 = $this->_randomPhase();
  109. $ph2 = $this->_randomPhase();
  110. $ph3 = $this->_randomPhase();
  111. $ph4 = $this->_randomPhase();
  112. $szx = $this->_randomSize();
  113. $szy = $this->_randomSize();
  114. for ($x = 0; $x < $w; $x++) {
  115. for ($y = 0; $y < $h; $y++) {
  116. $sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3));
  117. $sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4));
  118. if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {
  119. continue;
  120. } else {
  121. $color = (imagecolorat($img, $sx, $sy) >> 16) & 0xFF;
  122. $color_x = (imagecolorat($img, $sx + 1, $sy) >> 16) & 0xFF;
  123. $color_y = (imagecolorat($img, $sx, $sy + 1) >> 16) & 0xFF;
  124. $color_xy = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF;
  125. }
  126. if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {
  127. // ignore background
  128. continue;
  129. } elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {
  130. // transfer inside of the image as-is
  131. $newcolor = 0;
  132. } else {
  133. // do antialiasing for border items
  134. $frac_x = $sx-floor($sx);
  135. $frac_y = $sy-floor($sy);
  136. $frac_x1 = 1-$frac_x;
  137. $frac_y1 = 1-$frac_y;
  138. $newcolor = $color * $frac_x1 * $frac_y1
  139. + $color_x * $frac_x * $frac_y1
  140. + $color_y * $frac_x1 * $frac_y
  141. + $color_xy * $frac_x * $frac_y;
  142. }
  143. imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor));
  144. }
  145. }
  146. // generate noise
  147. for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
  148. imagefilledellipse($img2, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
  149. }
  150. for ($i=0; $i<$this->_lineNoiseLevel; $i++) {
  151. imageline($img2, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
  152. }
  153. imagealphablending($img2,false);
  154. imagesavealpha($img2,true);
  155. imagepng($img2, $img_file);
  156. imagedestroy($img);
  157. imagedestroy($img2);
  158. }
  159. /**
  160. * Display the captcha
  161. *
  162. * @param Zend_View_Interface $view
  163. * @param mixed $element
  164. * @return string
  165. */
  166. public function render(Zend_View_Interface $view = null, $element = null)
  167. {
  168. return '<img width="'.$this->getWidth().'" height="'.$this->getHeight().'" alt="'.$this->getImgAlt().'" src="' . $this->getImgUrl() . $this->getId() . $this->getSuffix() . '" id="captchaImageId"/>';
  169. }
  170. }