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

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

http://github.com/enormego/EightPHP
PHP | 86 lines | 37 code | 14 blank | 35 comment | 3 complexity | e08f0b8a97e0c51f8f550ed14326a672 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Captcha driver for "alpha" 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_Alpha_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, Captcha::$config['complexity']));
  20. }
  21. /**
  22. * Outputs the Captcha image.
  23. *
  24. * @param boolean html output
  25. * @return mixed
  26. */
  27. public function render($html) {
  28. // Creates $this->image
  29. $this->image_create(Captcha::$config['background']);
  30. // Add a random gradient
  31. if(empty(Captcha::$config['background'])) {
  32. $color1 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  33. $color2 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  34. $this->image_gradient($color1, $color2);
  35. }
  36. // Add a few random circles
  37. for($i = 0, $count = mt_rand(10, Captcha::$config['complexity'] * 3); $i < $count; $i++) {
  38. $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), mt_rand(80, 120));
  39. $size = mt_rand(5, Captcha::$config['height'] / 3);
  40. imagefilledellipse($this->image, mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), $size, $size, $color);
  41. }
  42. // Calculate character font-size and spacing
  43. $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / strlen($this->response);
  44. $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response));
  45. // Background alphabetic character attributes
  46. $color_limit = mt_rand(96, 160);
  47. $chars = 'ABEFGJKLPQRTVY';
  48. // Draw each Captcha character with varying attributes
  49. for($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++) {
  50. // Use different fonts if available
  51. $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
  52. $angle = mt_rand(-40, 20);
  53. // Scale the character size on image height
  54. $size = $default_size / 10 * mt_rand(8, 12);
  55. $box = imageftbbox($size, $angle, $font, $this->response[$i]);
  56. // Calculate character starting coordinates
  57. $x = $spacing / 4 + $i * $spacing;
  58. $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
  59. // Draw captcha text character
  60. // Allocate random color, size and rotation attributes to text
  61. $color = imagecolorallocate($this->image, mt_rand(150, 255), mt_rand(200, 255), mt_rand(0, 255));
  62. // Write text character to image
  63. imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]);
  64. // Draw "ghost" alphabetic character
  65. $text_color = imagecolorallocatealpha($this->image, mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand(70, 120));
  66. $char = substr($chars, mt_rand(0, 14), 1);
  67. imagettftext($this->image, $size * 2, mt_rand(-45, 45), ($x - (mt_rand(5, 10))), ($y + (mt_rand(5, 10))), $text_color, $font, $char);
  68. }
  69. // Output
  70. return $this->image_render($html);
  71. }
  72. } // End Captcha Alpha Driver Class