PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/vladimiraleksiev/mhbg
PHP | 186 lines | 111 code | 24 blank | 51 comment | 34 complexity | c8e9a7d767b784d2f3f0f1803b98424e MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Captcha
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Image.php 16971 2009-07-22 18:05:45Z mikaelkael $
  21. */
  22. /** Zend_Captcha_Word */
  23. require_once 'Zend/Captcha/Word.php';
  24. /**
  25. * Image-based captcha element
  26. *
  27. * Generates image displaying random word
  28. *
  29. * @category Zend
  30. * @package Zend_Captcha
  31. * @subpackage Adapter
  32. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Dnez_Captcha_Image_Transparent extends Zend_Captcha_Image
  36. {
  37. private $transparent = false;
  38. /**
  39. * Generate image captcha
  40. *
  41. * Override this function if you want different image generator
  42. * Wave transform from http://www.captcha.ru/captchas/multiwave/
  43. *
  44. * @param string $id Captcha ID
  45. * @param string $word Captcha word
  46. */
  47. protected function _generateImage($id, $word)
  48. {
  49. if (!extension_loaded("gd")) {
  50. require_once 'Zend/Captcha/Exception.php';
  51. throw new Zend_Captcha_Exception("Image CAPTCHA requires GD extension");
  52. }
  53. if (!function_exists("imagepng")) {
  54. require_once 'Zend/Captcha/Exception.php';
  55. throw new Zend_Captcha_Exception("Image CAPTCHA requires PNG support");
  56. }
  57. if (!function_exists("imageftbbox")) {
  58. require_once 'Zend/Captcha/Exception.php';
  59. throw new Zend_Captcha_Exception("Image CAPTCHA requires FT fonts support");
  60. }
  61. $font = $this->getFont();
  62. if (empty($font)) {
  63. require_once 'Zend/Captcha/Exception.php';
  64. throw new Zend_Captcha_Exception("Image CAPTCHA requires font");
  65. }
  66. $w = $this->getWidth();
  67. $h = $this->getHeight();
  68. $fsize = $this->getFontSize();
  69. $img_file = $this->getImgDir() . $id . $this->getSuffix();
  70. if(empty($this->_startImage)) {
  71. $img = imagecreatetruecolor($w, $h);
  72. } else {
  73. $img = imagecreatefrompng($this->_startImage);
  74. if(!$img) {
  75. require_once 'Zend/Captcha/Exception.php';
  76. throw new Zend_Captcha_Exception("Can not load start image");
  77. }
  78. $w = imagesx($img);
  79. $h = imagesy($img);
  80. }
  81. $text_color = imagecolorallocate($img, 0, 0, 0);
  82. imagealphablending($img, false);
  83. $bg_color = imagecolorallocatealpha($img,255,255,255,127);
  84. imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color);
  85. imagealphablending($img,true);
  86. $textbox = imageftbbox($fsize, 0, $font, $word);
  87. $x = ($w - ($textbox[2] - $textbox[0])) / 2;
  88. $y = ($h - ($textbox[7] - $textbox[1])) / 2;
  89. imagefttext($img, $fsize, 0, $x, $y, $text_color, $font, $word);
  90. // generate noise
  91. for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
  92. imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
  93. }
  94. for($i=0; $i<$this->_lineNoiseLevel; $i++) {
  95. imageline($img, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
  96. }
  97. // transformed image
  98. $img2 = imagecreatetruecolor($w, $h);
  99. imagealphablending($img2, false);
  100. $bg_color = imagecolorallocatealpha($img2,255,255,255,127);
  101. imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);
  102. imagealphablending($img2,true);
  103. // $bg_color = imagecolorallocate($img2, 255, 255, 255);
  104. // imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);
  105. // apply wave transforms
  106. $freq1 = $this->_randomFreq();
  107. $freq2 = $this->_randomFreq();
  108. $freq3 = $this->_randomFreq();
  109. $freq4 = $this->_randomFreq();
  110. $ph1 = $this->_randomPhase();
  111. $ph2 = $this->_randomPhase();
  112. $ph3 = $this->_randomPhase();
  113. $ph4 = $this->_randomPhase();
  114. $szx = $this->_randomSize();
  115. $szy = $this->_randomSize();
  116. for ($x = 0; $x < $w; $x++) {
  117. for ($y = 0; $y < $h; $y++) {
  118. $sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3)) * $szx;
  119. $sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4)) * $szy;
  120. if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {
  121. continue;
  122. } else {
  123. $color = (imagecolorat($img, $sx, $sy) >> 16) & 0xFF;
  124. $color_x = (imagecolorat($img, $sx + 1, $sy) >> 16) & 0xFF;
  125. $color_y = (imagecolorat($img, $sx, $sy + 1) >> 16) & 0xFF;
  126. $color_xy = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF;
  127. }
  128. if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {
  129. // ignore background
  130. continue;
  131. } elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {
  132. // transfer inside of the image as-is
  133. $newcolor = 0;
  134. } else {
  135. // do antialiasing for border items
  136. $frac_x = $sx-floor($sx);
  137. $frac_y = $sy-floor($sy);
  138. $frac_x1 = 1-$frac_x;
  139. $frac_y1 = 1-$frac_y;
  140. $newcolor = $color * $frac_x1 * $frac_y1
  141. + $color_x * $frac_x * $frac_y1
  142. + $color_y * $frac_x1 * $frac_y
  143. + $color_xy * $frac_x * $frac_y;
  144. }
  145. imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor));
  146. }
  147. }
  148. // generate noise
  149. for ($i=0; $i<$this->_dotNoiseLevel; $i++) {
  150. imagefilledellipse($img2, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
  151. }
  152. for ($i=0; $i<$this->_lineNoiseLevel; $i++) {
  153. imageline($img2, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
  154. }
  155. // imagealphablending($img,false);
  156. imagealphablending($img2,false);
  157. imagesavealpha($img2,true);
  158. imagepng($img2, $img_file);
  159. imagedestroy($img);
  160. imagedestroy($img2);
  161. }
  162. }