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

/modules/captcha/classes/captcha/black.php

https://bitbucket.org/seyar/startech.local
PHP | 74 lines | 36 code | 9 blank | 29 comment | 1 complexity | be76504fb2753c2fcbaaca037e01eafd MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access.');
  2. /**
  3. * Black captcha class.
  4. *
  5. * @package Captcha
  6. * @subpackage Captcha_Black
  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_Black 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, ceil(Captcha::$config['complexity'] / 1.5)));
  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 a black image to start from
  34. $this->image_create(Captcha::$config['background']);
  35. // Add random white/gray arcs, amount depends on complexity setting
  36. $count = (Captcha::$config['width'] + Captcha::$config['height']) / 2;
  37. $count = $count / 5 * min(10, Captcha::$config['complexity']);
  38. for ($i = 0; $i < $count; $i++)
  39. {
  40. imagesetthickness($this->image, mt_rand(1, 2));
  41. $color = imagecolorallocatealpha($this->image, 255, 255, 255, mt_rand(0, 120));
  42. imagearc($this->image, mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(0, 360), mt_rand(0, 360), $color);
  43. }
  44. // Use different fonts if available
  45. $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
  46. // Draw the character's white shadows
  47. $size = (int) min(Captcha::$config['height'] / 2, Captcha::$config['width'] * 0.8 / strlen($this->response));
  48. $angle = mt_rand(-15 + strlen($this->response), 15 - strlen($this->response));
  49. $x = mt_rand(1, Captcha::$config['width'] * 0.9 - $size * strlen($this->response));
  50. $y = ((Captcha::$config['height'] - $size) / 2) + $size;
  51. $color = imagecolorallocate($this->image, 255, 255, 255);
  52. imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, $font, $this->response);
  53. // Add more shadows for lower complexities
  54. (Captcha::$config['complexity'] < 10) and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, $font , $this->response);
  55. (Captcha::$config['complexity'] < 8) and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, $font , $this->response);
  56. (Captcha::$config['complexity'] < 6) and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, $font , $this->response);
  57. (Captcha::$config['complexity'] < 4) and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, $font , $this->response);
  58. (Captcha::$config['complexity'] < 2) and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, $font , $this->response);
  59. // Finally draw the foreground characters
  60. $color = imagecolorallocate($this->image, 0, 0, 0);
  61. imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response);
  62. // Output
  63. return $this->image_render($html);
  64. }
  65. } // End Captcha Black Driver Class