PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/captcha/classes/captcha/alpha.php

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