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

/cakesocial.local/app/vendors/zend/library/Zend/Captcha/Image.php

https://github.com/miamiruby/cakestuff
PHP | 494 lines | 220 code | 51 blank | 223 comment | 36 complexity | ab1a51d4e12fabd17422907cb1522f0e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, MIT
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Captcha_Word */
  22. require_once 'Zend/Captcha/Word.php';
  23. /**
  24. * Image-based captcha element
  25. *
  26. * Generates image displaying random word
  27. *
  28. * @category Zend
  29. * @package Zend_Captcha
  30. * @subpackage Adapter
  31. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @version $Id: $
  34. */
  35. class Zend_Captcha_Image extends Zend_Captcha_Word
  36. {
  37. /**
  38. * Directory for generated images
  39. *
  40. * @var string
  41. */
  42. protected $_imgDir = "./images/captcha/";
  43. /**
  44. * URL for accessing images
  45. *
  46. * @var string
  47. */
  48. protected $_imgUrl = "/images/captcha/";
  49. /**
  50. * Image suffix (including dot)
  51. *
  52. * @var string
  53. */
  54. protected $_suffix = ".png";
  55. /**
  56. * Image width
  57. *
  58. * @var int
  59. */
  60. protected $_width = 200;
  61. /**
  62. * Image height
  63. *
  64. * @var int
  65. */
  66. protected $_height = 50;
  67. /**
  68. * Font size
  69. *
  70. * @var int
  71. */
  72. protected $_fsize = 24;
  73. /**
  74. * Image font file
  75. *
  76. * @var string
  77. */
  78. protected $_font;
  79. /**
  80. * How frequently to execute garbage collection
  81. *
  82. * @var int
  83. */
  84. protected $_gcFreq = 10;
  85. /**
  86. * How long to keep generated images
  87. *
  88. * @var int
  89. */
  90. protected $_expiration = 600;
  91. /**
  92. * Get captcha expiration
  93. *
  94. * @return int
  95. */
  96. public function getExpiration()
  97. {
  98. return $this->_expiration;
  99. }
  100. /**
  101. * Get garbage collection frequency
  102. *
  103. * @return int
  104. */
  105. public function getGcFreq()
  106. {
  107. return $this->_gcFreq;
  108. }
  109. /**
  110. * Set captcha expiration
  111. *
  112. * @param int $expiration
  113. * @return Zend_Captcha_Image
  114. */
  115. public function setExpiration($expiration)
  116. {
  117. $this->_expiration = $expiration;
  118. return $this;
  119. }
  120. /**
  121. * Set garbage collection frequency
  122. *
  123. * @param int $gcFreq
  124. * @return Zend_Captcha_Image
  125. */
  126. public function setGcFreq($gcFreq)
  127. {
  128. $this->_gcFreq = $gcFreq;
  129. return $this;
  130. }
  131. /**
  132. * Get font to use when generating captcha
  133. *
  134. * @return string
  135. */
  136. public function getFont()
  137. {
  138. return $this->_font;
  139. }
  140. /**
  141. * Get font size
  142. *
  143. * @return int
  144. */
  145. public function getFontSize()
  146. {
  147. return $this->_fsize;
  148. }
  149. /**
  150. * Get captcha image height
  151. *
  152. * @return int
  153. */
  154. public function getHeight()
  155. {
  156. return $this->_height;
  157. }
  158. /**
  159. * Get captcha image directory
  160. *
  161. * @return string
  162. */
  163. public function getImgDir()
  164. {
  165. return $this->_imgDir;
  166. }
  167. /**
  168. * Get captcha image base URL
  169. *
  170. * @return string
  171. */
  172. public function getImgUrl()
  173. {
  174. return $this->_imgUrl;
  175. }
  176. /**
  177. * Get captcha image file suffix
  178. *
  179. * @return string
  180. */
  181. public function getSuffix()
  182. {
  183. return $this->_suffix;
  184. }
  185. /**
  186. * Get captcha image width
  187. *
  188. * @return int
  189. */
  190. public function getWidth()
  191. {
  192. return $this->_width;
  193. }
  194. /**
  195. * Set captcha font
  196. *
  197. * @param string $font
  198. * @return Zend_Captcha_Image
  199. */
  200. public function setFont($font)
  201. {
  202. $this->_font = $font;
  203. return $this;
  204. }
  205. /**
  206. * Set captcha font size
  207. *
  208. * @param int $fsize
  209. * @return Zend_Captcha_Image
  210. */
  211. public function setFontSize($fsize)
  212. {
  213. $this->_fsize = $fsize;
  214. return $this;
  215. }
  216. /**
  217. * Set captcha image height
  218. *
  219. * @param int $height
  220. * @return Zend_Captcha_Image
  221. */
  222. public function setHeight($height)
  223. {
  224. $this->_height = $height;
  225. return $this;
  226. }
  227. /**
  228. * Set captcha image storage directory
  229. *
  230. * @param string $imgDir
  231. * @return Zend_Captcha_Image
  232. */
  233. public function setImgDir($imgDir)
  234. {
  235. $this->_imgDir = rtrim($imgDir, "/\\") . '/';
  236. return $this;
  237. }
  238. /**
  239. * Set captcha image base URL
  240. *
  241. * @param string $imgUrl
  242. * @return Zend_Captcha_Image
  243. */
  244. public function setImgUrl($imgUrl)
  245. {
  246. $this->_imgUrl = rtrim($imgUrl, "/\\") . '/';
  247. return $this;
  248. }
  249. /**
  250. * Set captch image filename suffix
  251. *
  252. * @param string $suffix
  253. * @return Zend_Captcha_Image
  254. */
  255. public function setSuffix($suffix)
  256. {
  257. $this->_suffix = $suffix;
  258. return $this;
  259. }
  260. /**
  261. * Set captcha image width
  262. *
  263. * @param int $width
  264. * @return Zend_Captcha_Image
  265. */
  266. public function setWidth($width)
  267. {
  268. $this->_width = $width;
  269. return $this;
  270. }
  271. /**
  272. * Generate random frequency
  273. *
  274. * @return float
  275. */
  276. protected function _randomFreq()
  277. {
  278. return mt_rand(700000, 1000000) / 15000000;
  279. }
  280. /**
  281. * Generate random phase
  282. *
  283. * @return float
  284. */
  285. protected function _randomPhase()
  286. {
  287. // random phase from 0 to pi
  288. return mt_rand(0, 3141592) / 1000000;
  289. }
  290. /**
  291. * Generate random character size
  292. *
  293. * @return int
  294. */
  295. protected function _randomSize()
  296. {
  297. return mt_rand(300, 700) / 100;
  298. }
  299. /**
  300. * Generate captcha
  301. *
  302. * @return string captcha ID
  303. */
  304. public function generate()
  305. {
  306. $id = parent::generate();
  307. $this->_generateImage($id, $this->getWord());
  308. if (mt_rand(0, $this->getGcFreq()) == 1) {
  309. $this->_gc();
  310. }
  311. return $id;
  312. }
  313. /**
  314. * Generate image captcha
  315. *
  316. * Override this function if you want different image generator
  317. *
  318. * @param string $id Captcha ID
  319. * @param string $word Captcha word
  320. */
  321. protected function _generateImage($id, $word)
  322. {
  323. if (!extension_loaded("gd")) {
  324. require_once 'Zend/Form/Exception.php';
  325. throw new Zend_Form_Exception("Image CAPTCHA requires GD extension");
  326. }
  327. if (!function_exists("imagepng")) {
  328. require_once 'Zend/Form/Exception.php';
  329. throw new Zend_Form_Exception("Image CAPTCHA requires PNG support");
  330. }
  331. if (!function_exists("imageftbbox")) {
  332. require_once 'Zend/Form/Exception.php';
  333. throw new Zend_Form_Exception("Image CAPTCHA requires FT fonts support");
  334. }
  335. $font = $this->getFont();
  336. if (empty($font)) {
  337. require_once 'Zend/Form/Exception.php';
  338. throw new Zend_Form_Exception("Image CAPTCHA requires font");
  339. }
  340. $w = $this->getWidth();
  341. $h = $this->getHeight();
  342. $fsize = $this->getFontSize();
  343. $img_file = $this->getImgDir() . $id . $this->getSuffix();
  344. $img = imagecreatetruecolor($w, $h);
  345. $text_color = imagecolorallocate($img, 0, 0, 0);
  346. $bg_color = imagecolorallocate($img, 255, 255, 255);
  347. imagefilledrectangle($img, 0, 0, $w-1, $h-1, $bg_color);
  348. $textbox = imageftbbox($fsize, 0, $font, $word);
  349. $x = ($w - ($textbox[2] - $textbox[0])) / 2;
  350. $y = ($h - ($textbox[7] - $textbox[1])) / 2;
  351. imagefttext($img, $fsize, 0, $x, $y, $text_color, $font, $word);
  352. // generate noise
  353. for ($i=0; $i<100; $i++) {
  354. imagefilledellipse($img, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
  355. }
  356. for($i=0; $i<5; $i++) {
  357. imageline($img, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
  358. }
  359. // transformed image
  360. $img2 = imagecreatetruecolor($w, $h);
  361. $bg_color = imagecolorallocate($img2, 255, 255, 255);
  362. imagefilledrectangle($img2, 0, 0, $w-1, $h-1, $bg_color);
  363. // apply wave transforms
  364. $freq1 = $this->_randomFreq();
  365. $freq2 = $this->_randomFreq();
  366. $freq3 = $this->_randomFreq();
  367. $freq4 = $this->_randomFreq();
  368. $ph1 = $this->_randomPhase();
  369. $ph2 = $this->_randomPhase();
  370. $ph3 = $this->_randomPhase();
  371. $ph4 = $this->_randomPhase();
  372. $szx = $this->_randomSize();
  373. $szy = $this->_randomSize();
  374. for ($x = 0; $x < $w; $x++) {
  375. for ($y = 0; $y < $h; $y++) {
  376. $sx = $x + (sin($x*$freq1 + $ph1) + sin($y*$freq3 + $ph3)) * $szx;
  377. $sy = $y + (sin($x*$freq2 + $ph2) + sin($y*$freq4 + $ph4)) * $szy;
  378. if ($sx < 0 || $sy < 0 || $sx >= $w - 1 || $sy >= $h - 1) {
  379. continue;
  380. } else {
  381. $color = (imagecolorat($img, $sx, $sy) >> 16) & 0xFF;
  382. $color_x = (imagecolorat($img, $sx + 1, $sy) >> 16) & 0xFF;
  383. $color_y = (imagecolorat($img, $sx, $sy + 1) >> 16) & 0xFF;
  384. $color_xy = (imagecolorat($img, $sx + 1, $sy + 1) >> 16) & 0xFF;
  385. }
  386. if ($color == 255 && $color_x == 255 && $color_y == 255 && $color_xy == 255) {
  387. // ignore background
  388. continue;
  389. } elseif ($color == 0 && $color_x == 0 && $color_y == 0 && $color_xy == 0) {
  390. // transfer inside of the image as-is
  391. $newcolor = 0;
  392. } else {
  393. // do antialiasing for border items
  394. $frac_x = $sx-floor($sx);
  395. $frac_y = $sy-floor($sy);
  396. $frac_x1 = 1-$frac_x;
  397. $frac_y1 = 1-$frac_y;
  398. $newcolor = $color * $frac_x1 * $frac_y1
  399. + $color_x * $frac_x * $frac_y1
  400. + $color_y * $frac_x1 * $frac_y
  401. + $color_xy * $frac_x * $frac_y;
  402. }
  403. imagesetpixel($img2, $x, $y, imagecolorallocate($img2, $newcolor, $newcolor, $newcolor));
  404. }
  405. }
  406. // generate noise
  407. for ($i=0; $i<100; $i++) {
  408. imagefilledellipse($img2, mt_rand(0,$w), mt_rand(0,$h), 2, 2, $text_color);
  409. }
  410. for ($i=0; $i<5; $i++) {
  411. imageline($img2, mt_rand(0,$w), mt_rand(0,$h), mt_rand(0,$w), mt_rand(0,$h), $text_color);
  412. }
  413. imagepng($img2, $img_file);
  414. imagedestroy($img);
  415. imagedestroy($img2);
  416. }
  417. /**
  418. * Remove old files from image directory
  419. *
  420. */
  421. protected function _gc()
  422. {
  423. $expire = time() - $this->getExpiration();
  424. foreach (new DirectoryIterator($this->getImgDir()) as $file) {
  425. if (!$file->isDot() && !$file->isDir()) {
  426. if ($file->getMTime() < $expire) {
  427. unlink($file->getPathname());
  428. }
  429. }
  430. }
  431. }
  432. /**
  433. * Display the captcha
  434. *
  435. * @param Zend_View $view
  436. * @param mixed $element
  437. * @return string
  438. */
  439. public function render(Zend_View $view, $element = null)
  440. {
  441. return '<img src="' . $this->getImgUrl() . $this->getId() . $this->getSuffix() . '"/><br/>';
  442. }
  443. }