PageRenderTime 56ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/enormego/EightPHP
PHP | 75 lines | 31 code | 12 blank | 32 comment | 3 complexity | c44c23587ba1d06d5f16a60c7ee44404 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Captcha driver for "basic" 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_Basic_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(200, 255), mt_rand(200, 255), mt_rand(150, 255));
  33. $color2 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
  34. $this->image_gradient($color1, $color2);
  35. }
  36. // Add a few random lines
  37. for($i = 0, $count = mt_rand(5, Captcha::$config['complexity'] * 4); $i < $count; $i++) {
  38. $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(100, 255), mt_rand(50, 120));
  39. imageline($this->image, mt_rand(0, Captcha::$config['width']), 0, mt_rand(0, Captcha::$config['width']), Captcha::$config['height'], $color);
  40. }
  41. // Calculate character font-size and spacing
  42. $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / (strlen($this->response) + 1);
  43. $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response));
  44. // Draw each Captcha character with varying attributes
  45. for($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++) {
  46. // Use different fonts if available
  47. $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
  48. // Allocate random color, size and rotation attributes to text
  49. $color = imagecolorallocate($this->image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
  50. $angle = mt_rand(-40, 20);
  51. // Scale the character size on image height
  52. $size = $default_size / 10 * mt_rand(8, 12);
  53. $box = imageftbbox($size, $angle, $font, $this->response[$i]);
  54. // Calculate character starting coordinates
  55. $x = $spacing / 4 + $i * $spacing;
  56. $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
  57. // Write text character to image
  58. imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]);
  59. }
  60. // Output
  61. return $this->image_render($html);
  62. }
  63. } // End Captcha Basic Driver Class