PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/captcha/classes/libraries/captcha/driver/black.php

http://github.com/enormego/EightPHP
PHP | 68 lines | 31 code | 9 blank | 28 comment | 1 complexity | 2e5d68e9beb9c3e5b9453cd4f130597b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Captcha driver for "black" style.
  4. *
  5. * @package Modules
  6. * @subpackage Captcha
  7. * @author EightPHP Development Team
  8. * @copyright (c) 2009-2010 EightPHP
  9. * @license http://license.eightphp.com
  10. */
  11. class Captcha_Driver_Black_Core extends Captcha_Driver {
  12. /**
  13. * Generates a new Captcha challenge.
  14. *
  15. * @return string the challenge answer
  16. */
  17. public function generate_challenge() {
  18. // Complexity setting is used as character count
  19. return str::random('distinct', max(1, ceil(Captcha::$config['complexity'] / 1.5)));
  20. }
  21. /**
  22. * Outputs the Captcha image.
  23. *
  24. * @param boolean html output
  25. * @return mixed
  26. */
  27. public function render($html) {
  28. // Creates a black image to start from
  29. $this->image_create(Captcha::$config['background']);
  30. // Add random white/gray arcs, amount depends on complexity setting
  31. $count = (Captcha::$config['width'] + Captcha::$config['height']) / 2;
  32. $count = $count / 5 * min(10, Captcha::$config['complexity']);
  33. for($i = 0; $i < $count; $i++) {
  34. imagesetthickness($this->image, mt_rand(1, 2));
  35. $color = imagecolorallocatealpha($this->image, 255, 255, 255, mt_rand(0, 120));
  36. 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);
  37. }
  38. // Use different fonts if available
  39. $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
  40. // Draw the character's white shadows
  41. $size = (int) min(Captcha::$config['height'] / 2, Captcha::$config['width'] * 0.8 / strlen($this->response));
  42. $angle = mt_rand(-15 + strlen($this->response), 15 - strlen($this->response));
  43. $x = mt_rand(1, Captcha::$config['width'] * 0.9 - $size * strlen($this->response));
  44. $y = ((Captcha::$config['height'] - $size) / 2) + $size;
  45. $color = imagecolorallocate($this->image, 255, 255, 255);
  46. imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, $font, $this->response);
  47. // Add more shadows for lower complexities
  48. (Captcha::$config['complexity'] < 10) and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, $font , $this->response);
  49. (Captcha::$config['complexity'] < 8) and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, $font , $this->response);
  50. (Captcha::$config['complexity'] < 6) and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, $font , $this->response);
  51. (Captcha::$config['complexity'] < 4) and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, $font , $this->response);
  52. (Captcha::$config['complexity'] < 2) and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, $font , $this->response);
  53. // Finally draw the foreground characters
  54. $color = imagecolorallocate($this->image, 0, 0, 0);
  55. imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response);
  56. // Output
  57. return $this->image_render($html);
  58. }
  59. } // End Captcha Black Driver Class