PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/drivers/Captcha/Basic.php

https://github.com/Toushi/flow
PHP | 81 lines | 36 code | 12 blank | 33 comment | 3 complexity | fa22c3e8e30c2ab1a4a7cc6e2363cd01 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Captcha driver for "basic" style.
  4. *
  5. * $Id: Basic.php 3104 2008-07-13 12:06:52Z 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_Basic_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, Captcha::$config['complexity']));
  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 $this->image
  32. $this->image_create(Captcha::$config['background']);
  33. // Add a random gradient
  34. if (empty(Captcha::$config['background']))
  35. {
  36. $color1 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
  37. $color2 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
  38. $this->image_gradient($color1, $color2);
  39. }
  40. // Add a few random lines
  41. for ($i = 0, $count = mt_rand(5, Captcha::$config['complexity'] * 4); $i < $count; $i++)
  42. {
  43. $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(100, 255), mt_rand(50, 120));
  44. imageline($this->image, mt_rand(0, Captcha::$config['width']), 0, mt_rand(0, Captcha::$config['width']), Captcha::$config['height'], $color);
  45. }
  46. // Calculate character font-size and spacing
  47. $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / (strlen($this->response) + 1);
  48. $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response));
  49. // Draw each Captcha character with varying attributes
  50. for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++)
  51. {
  52. // Use different fonts if available
  53. $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
  54. // Allocate random color, size and rotation attributes to text
  55. $color = imagecolorallocate($this->image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
  56. $angle = mt_rand(-40, 20);
  57. // Scale the character size on image height
  58. $size = $default_size / 10 * mt_rand(8, 12);
  59. $box = imageftbbox($size, $angle, $font, $this->response[$i]);
  60. // Calculate character starting coordinates
  61. $x = $spacing / 4 + $i * $spacing;
  62. $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
  63. // Write text character to image
  64. imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]);
  65. }
  66. // Output
  67. return $this->image_render($html);
  68. }
  69. } // End Captcha Basic Driver Class