PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/captcha/black.php

https://github.com/ascseb/kohana-captcha
PHP | 72 lines | 36 code | 9 blank | 27 comment | 1 complexity | 21e5a295e1bea37268398472f6959f80 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access.');
  2. /**
  3. * Black 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_Black 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, ceil(Captcha::$config['complexity'] / 1.5)));
  21. return $text;
  22. }
  23. /**
  24. * Outputs the Captcha image.
  25. *
  26. * @param boolean html output
  27. * @return mixed
  28. */
  29. public function render($html = TRUE)
  30. {
  31. // Creates a black image to start from
  32. $this->image_create(Captcha::$config['background']);
  33. // Add random white/gray arcs, amount depends on complexity setting
  34. $count = (Captcha::$config['width'] + Captcha::$config['height']) / 2;
  35. $count = $count / 5 * min(10, Captcha::$config['complexity']);
  36. for ($i = 0; $i < $count; $i++)
  37. {
  38. imagesetthickness($this->image, mt_rand(1, 2));
  39. $color = imagecolorallocatealpha($this->image, 255, 255, 255, mt_rand(0, 120));
  40. 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);
  41. }
  42. // Use different fonts if available
  43. $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
  44. // Draw the character's white shadows
  45. $size = (int) min(Captcha::$config['height'] / 2, Captcha::$config['width'] * 0.8 / strlen($this->response));
  46. $angle = mt_rand(-15 + strlen($this->response), 15 - strlen($this->response));
  47. $x = mt_rand(1, Captcha::$config['width'] * 0.9 - $size * strlen($this->response));
  48. $y = ((Captcha::$config['height'] - $size) / 2) + $size;
  49. $color = imagecolorallocate($this->image, 255, 255, 255);
  50. imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, $font, $this->response);
  51. // Add more shadows for lower complexities
  52. (Captcha::$config['complexity'] < 10) and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, $font , $this->response);
  53. (Captcha::$config['complexity'] < 8) and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, $font , $this->response);
  54. (Captcha::$config['complexity'] < 6) and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, $font , $this->response);
  55. (Captcha::$config['complexity'] < 4) and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, $font , $this->response);
  56. (Captcha::$config['complexity'] < 2) and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, $font , $this->response);
  57. // Finally draw the foreground characters
  58. $color = imagecolorallocate($this->image, 0, 0, 0);
  59. imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response);
  60. // Output
  61. return $this->image_render($html);
  62. }
  63. } // End Captcha Black Driver Class