Image.php 8.8 KB

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