Image.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Require Image_Text class for generating the text.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Text
  8. * @package Text_CAPTCHA
  9. * @author Christian Wenz <wenz@php.net>
  10. * @author Michael Cramer <michael@bigmichi1.de>
  11. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  12. * @link http://pear.php.net/package/Text_CAPTCHA
  13. */
  14. /**
  15. * Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs
  16. *
  17. * Class to create a graphical Turing test
  18. *
  19. * @category Text
  20. * @package Text_CAPTCHA
  21. * @author Christian Wenz <wenz@php.net>
  22. * @author Michael Cramer <michael@bigmichi1.de>
  23. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  24. * @link http://pear.php.net/package/Text_CAPTCHA
  25. * @todo refine the obfuscation algorithm :-)
  26. * @todo consider removing Image_Text dependency
  27. */
  28. class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA_Driver_Base
  29. {
  30. /**
  31. * Text_Password options.
  32. *
  33. * @var array
  34. */
  35. private $_textPasswordOptions;
  36. /**
  37. * Width of CAPTCHA
  38. *
  39. * @var int
  40. */
  41. private $_width;
  42. /**
  43. * Height of CAPTCHA
  44. *
  45. * @var int
  46. */
  47. private $_height;
  48. /**
  49. * CAPTCHA output format
  50. *
  51. * @var string
  52. */
  53. private $_output;
  54. /**
  55. * Further options (here: for Image_Text)
  56. *
  57. * @var array
  58. */
  59. private $_imageOptions = array(
  60. 'font_size' => 24,
  61. 'font_path' => './',
  62. 'font_file' => 'COUR.TTF',
  63. 'text_color' => '#000000',
  64. 'lines_color' => '#CACACA',
  65. 'background_color' => '#555555',
  66. 'antialias' => false
  67. );
  68. /**
  69. * Init function
  70. *
  71. * Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image
  72. *
  73. * @param array $options CAPTCHA options
  74. *
  75. * @return void
  76. */
  77. public function initDriver($options = array())
  78. {
  79. if (is_array($options)) {
  80. if (isset($options['width']) && is_int($options['width'])) {
  81. $this->_width = $options['width'];
  82. } else {
  83. $this->_width = 200;
  84. }
  85. if (isset($options['height']) && is_int($options['height'])) {
  86. $this->_height = $options['height'];
  87. } else {
  88. $this->_height = 80;
  89. }
  90. if (!isset($options['phrase']) || empty($options['phrase'])) {
  91. $phraseOptions = (isset($options['phraseOptions'])
  92. && is_array($options['phraseOptions']))
  93. ? $options['phraseOptions'] : array();
  94. $this->_textPasswordOptions = $phraseOptions;
  95. } else {
  96. $this->setPhrase($options['phrase']);
  97. }
  98. if (!isset($options['output']) || empty($options['output'])) {
  99. $this->_output = 'jpeg';
  100. } else {
  101. $this->_output = $options['output'];
  102. }
  103. if (isset($options['imageOptions'])
  104. && is_array($options['imageOptions'])
  105. && count($options['imageOptions']) > 0
  106. ) {
  107. $this->_imageOptions = array_merge(
  108. $this->_imageOptions, $options['imageOptions']
  109. );
  110. }
  111. }
  112. }
  113. /**
  114. * Create CAPTCHA image.
  115. *
  116. * This method creates a CAPTCHA image.
  117. *
  118. * @return void
  119. * @throws Text_CAPTCHA_Exception when image generation with Image_Text produces
  120. * an error
  121. */
  122. public function createCAPTCHA()
  123. {
  124. $options['canvas'] = array(
  125. 'width' => $this->_width,
  126. 'height' => $this->_height
  127. );
  128. $options['width'] = $this->_width - 20;
  129. $options['height'] = $this->_height - 20;
  130. $options['cx'] = ceil(($this->_width) / 2 + 10);
  131. $options['cy'] = ceil(($this->_height) / 2 + 10);
  132. $options['angle'] = rand(0, 30) - 15;
  133. $options['font_size'] = $this->_imageOptions['font_size'];
  134. $options['font_path'] = $this->_imageOptions['font_path'];
  135. $options['font_file'] = $this->_imageOptions['font_file'];
  136. $options['color'] = array($this->_imageOptions['text_color']);
  137. $options['background_color'] = $this->_imageOptions['background_color'];
  138. $options['max_lines'] = 1;
  139. $options['mode'] = 'auto';
  140. do {
  141. $imageText = new Image_Text($this->getPhrase(), $options);
  142. $imageText->init();
  143. $result = $imageText->measurize();
  144. } while ($result === false && --$options['font_size'] > 0);
  145. if ($result === false) {
  146. throw new Text_CAPTCHA_Exception(
  147. 'The text provided does not fit in the image dimensions'
  148. );
  149. }
  150. $imageText->render();
  151. $image = $imageText->getImg();
  152. if ($this->_imageOptions['antialias'] && function_exists('imageantialias')) {
  153. imageantialias($image, true);
  154. }
  155. $colors = Image_Text::convertString2RGB(
  156. $this->_imageOptions['lines_color']
  157. );
  158. $linesColor = imagecolorallocate(
  159. $image, $colors['r'], $colors['g'], $colors['b']
  160. );
  161. //some obfuscation
  162. for ($i = 0; $i < 3; $i++) {
  163. $x1 = rand(0, $this->_width - 1);
  164. $y1 = rand(0, round($this->_height / 10, 0));
  165. $x2 = rand(0, round($this->_width / 10, 0));
  166. $y2 = rand(0, $this->_height - 1);
  167. imageline($image, $x1, $y1, $x2, $y2, $linesColor);
  168. $x1 = rand(0, $this->_width - 1);
  169. $y1 = $this->_height - rand(1, round($this->_height / 10, 0));
  170. $x2 = $this->_width - rand(1, round($this->_width / 10, 0));
  171. $y2 = rand(0, $this->_height - 1);
  172. imageline($image, $x1, $y1, $x2, $y2, $linesColor);
  173. $cx = rand(0, $this->_width - 50) + 25;
  174. $cy = rand(0, $this->_height - 50) + 25;
  175. $w = rand(1, 24);
  176. imagearc($image, $cx, $cy, $w, $w, 0, 360, $linesColor);
  177. }
  178. // @todo remove hardcoded value
  179. $this->_output = 'jpg';
  180. if ($this->_output == 'gif' && imagetypes() & IMG_GIF) {
  181. $this->setCaptcha($this->_getCAPTCHAAsGIF($image));
  182. } else if (($this->_output == 'jpg' && imagetypes() & IMG_JPG)
  183. || ($this->_output == 'jpeg' && imagetypes() & IMG_JPEG)
  184. ) {
  185. $this->setCaptcha($this->_getCAPTCHAAsJPEG($image));
  186. } else if ($this->_output == 'png' && imagetypes() & IMG_PNG) {
  187. $this->setCaptcha($this->_getCAPTCHAAsPNG($image));
  188. } else if ($this->_output == 'resource') {
  189. $this->setCaptcha($image);
  190. } else {
  191. throw new Text_CAPTCHA_Exception(
  192. "Unknown or unsupported output type specified"
  193. );
  194. }
  195. imagedestroy($image);
  196. }
  197. /**
  198. * Return CAPTCHA as PNG.
  199. *
  200. * This method returns the CAPTCHA as PNG.
  201. *
  202. * @param resource $image generated image
  203. *
  204. * @return string image contents
  205. */
  206. private function _getCAPTCHAAsPNG($image)
  207. {
  208. ob_start();
  209. imagepng($image);
  210. $data = ob_get_contents();
  211. ob_end_clean();
  212. return $data;
  213. }
  214. /**
  215. * Return CAPTCHA as JPEG.
  216. *
  217. * This method returns the CAPTCHA as JPEG.
  218. *
  219. * @param resource $image generated image
  220. *
  221. * @return string image contents
  222. */
  223. private function _getCAPTCHAAsJPEG($image)
  224. {
  225. ob_start();
  226. imagejpeg($image);
  227. $data = ob_get_contents();
  228. ob_end_clean();
  229. return $data;
  230. }
  231. /**
  232. * Return CAPTCHA as GIF.
  233. *
  234. * This method returns the CAPTCHA as GIF.
  235. *
  236. * @param resource $image generated image
  237. *
  238. * @return string image contents
  239. */
  240. private function _getCAPTCHAAsGIF($image)
  241. {
  242. ob_start();
  243. imagegif($image);
  244. $data = ob_get_contents();
  245. ob_end_clean();
  246. return $data;
  247. }
  248. /**
  249. * Create random CAPTCHA phrase, Image edition (with size check).
  250. *
  251. * This method creates a random phrase, maximum 8 characters or width / 25,
  252. * whatever is smaller.
  253. *
  254. * @return void
  255. */
  256. public function createPhrase()
  257. {
  258. $len = intval(min(8, $this->_width / 25));
  259. $options = $this->_textPasswordOptions;
  260. $textPassword = new Text_Password();
  261. if (!is_array($options) || count($options) === 0) {
  262. $this->setPhrase($textPassword->create($len));
  263. } else {
  264. if (count($options) === 1) {
  265. $this->setPhrase($textPassword->create($len, $options[0]));
  266. } else {
  267. $this->setPhrase(
  268. $textPassword->create($len, $options[0], $options[1])
  269. );
  270. }
  271. }
  272. }
  273. }