PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/captcha/alpha.php

https://github.com/ascseb/kohana-captcha
PHP | 93 lines | 44 code | 14 blank | 35 comment | 3 complexity | cb2b6c2ea6ccf146b25fdcfb7f46d478 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access.');
  2. /**
  3. * Alpha captcha class.
  4. *
  5. * @package Captcha
  6. * @author Kohana Team
  7. * @copyright (c) 2008-2009 Kohana Team
  8. * @license http://kohanaphp.com/license.html
  9. */
  10. class Captcha_Alpha extends Captcha
  11. {
  12. /**
  13. * Generates a new Captcha challenge.
  14. *
  15. * @return string the challenge answer
  16. */
  17. public function generate_challenge()
  18. {
  19. // Complexity setting is used as character count
  20. $text = text::random('distinct', max(1, Captcha::$config['complexity']));
  21. // Complexity setting is used as character count
  22. return $text;
  23. }
  24. /**
  25. * Outputs the Captcha image.
  26. *
  27. * @param boolean html output
  28. * @return mixed
  29. */
  30. public function render($html = TRUE)
  31. {
  32. // Creates $this->image
  33. $this->image_create(Captcha::$config['background']);
  34. // Add a random gradient
  35. if (empty(Captcha::$config['background']))
  36. {
  37. $color1 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  38. $color2 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  39. $this->image_gradient($color1, $color2);
  40. }
  41. // Add a few random circles
  42. for ($i = 0, $count = mt_rand(10, Captcha::$config['complexity'] * 3); $i < $count; $i++)
  43. {
  44. $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), mt_rand(80, 120));
  45. $size = mt_rand(5, Captcha::$config['height'] / 3);
  46. imagefilledellipse($this->image, mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), $size, $size, $color);
  47. }
  48. // Calculate character font-size and spacing
  49. $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / strlen($this->response);
  50. $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response));
  51. // Background alphabetic character attributes
  52. $color_limit = mt_rand(96, 160);
  53. $chars = 'ABEFGJKLPQRTVY';
  54. // Draw each Captcha character with varying attributes
  55. for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++)
  56. {
  57. // Use different fonts if available
  58. $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
  59. $angle = mt_rand(-40, 20);
  60. // Scale the character size on image height
  61. $size = $default_size / 10 * mt_rand(8, 12);
  62. $box = imageftbbox($size, $angle, $font, $this->response[$i]);
  63. // Calculate character starting coordinates
  64. $x = $spacing / 4 + $i * $spacing;
  65. $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
  66. // Draw captcha text character
  67. // Allocate random color, size and rotation attributes to text
  68. $color = imagecolorallocate($this->image, mt_rand(150, 255), mt_rand(200, 255), mt_rand(0, 255));
  69. // Write text character to image
  70. imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]);
  71. // Draw "ghost" alphabetic character
  72. $text_color = imagecolorallocatealpha($this->image, mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand(70, 120));
  73. $char = $chars[mt_rand(0, 13)];
  74. imagettftext($this->image, $size * 2, mt_rand(-45, 45), ($x - (mt_rand(5, 10))), ($y + (mt_rand(5, 10))), $text_color, $font, $char);
  75. }
  76. // Output
  77. return $this->image_render($html);
  78. }
  79. } // End Captcha Alpha Driver Class