PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/captcha/classes/captcha/basic.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 83 lines | 38 code | 12 blank | 33 comment | 3 complexity | d93b8b40a1e1dc4188b7340faede07ef MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access.');
  2. /**
  3. * Basic captcha class.
  4. *
  5. * @package Captcha
  6. * @subpackage Captcha_Basic
  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_Basic 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. return $text;
  24. }
  25. /**
  26. * Outputs the Captcha image.
  27. *
  28. * @param boolean $html HTML output
  29. * @return mixed
  30. */
  31. public function render($html = TRUE)
  32. {
  33. // Creates $this->image
  34. $this->image_create(Captcha::$config['background']);
  35. // Add a random gradient
  36. if (empty(Captcha::$config['background']))
  37. {
  38. $color1 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
  39. $color2 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
  40. $this->image_gradient($color1, $color2);
  41. }
  42. // Add a few random lines
  43. for ($i = 0, $count = mt_rand(5, Captcha::$config['complexity'] * 4); $i < $count; $i++)
  44. {
  45. $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(100, 255), mt_rand(50, 120));
  46. imageline($this->image, mt_rand(0, Captcha::$config['width']), 0, mt_rand(0, Captcha::$config['width']), Captcha::$config['height'], $color);
  47. }
  48. // Calculate character font-size and spacing
  49. $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / (strlen($this->response) + 1);
  50. $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response));
  51. // Draw each Captcha character with varying attributes
  52. for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++)
  53. {
  54. // Use different fonts if available
  55. $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
  56. // Allocate random color, size and rotation attributes to text
  57. $color = imagecolorallocate($this->image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
  58. $angle = mt_rand(-40, 20);
  59. // Scale the character size on image height
  60. $size = $default_size / 10 * mt_rand(8, 12);
  61. $box = imageftbbox($size, $angle, $font, $this->response[$i]);
  62. // Calculate character starting coordinates
  63. $x = $spacing / 4 + $i * $spacing;
  64. $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
  65. // Write text character to image
  66. imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]);
  67. }
  68. // Output
  69. return $this->image_render($html);
  70. }
  71. } // End Captcha Basic Driver Class