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

/system/libraries/drivers/Captcha/Black.php

https://github.com/Toushi/flow
PHP | 72 lines | 34 code | 9 blank | 29 comment | 1 complexity | 25296947bbe4b7a63a78e2836b012f8f MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Captcha driver for "black" style.
  4. *
  5. * $Id: Black.php 3103 2008-07-13 10:37:32Z Geert $
  6. *
  7. * @package Captcha
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class Captcha_Black_Driver extends Captcha_Driver {
  13. /**
  14. * Generates a new Captcha challenge.
  15. *
  16. * @return string the challenge answer
  17. */
  18. public function generate_challenge()
  19. {
  20. // Complexity setting is used as character count
  21. return text::random('distinct', max(1, ceil(Captcha::$config['complexity'] / 1.5)));
  22. }
  23. /**
  24. * Outputs the Captcha image.
  25. *
  26. * @param boolean html output
  27. * @return mixed
  28. */
  29. public function render($html)
  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