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

/system/libraries/drivers/Captcha/Alpha.php

https://github.com/ShakeNBake/gallery3
PHP | 92 lines | 42 code | 14 blank | 36 comment | 3 complexity | cd9ea83df637099807a3dc88326fc6f9 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Captcha driver for "alpha" style.
  4. *
  5. * $Id: Alpha.php 4367 2009-05-27 21:23:57Z samsoir $
  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_Alpha_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(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  37. $color2 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  38. $this->image_gradient($color1, $color2);
  39. }
  40. // Add a few random circles
  41. for ($i = 0, $count = mt_rand(10, Captcha::$config['complexity'] * 3); $i < $count; $i++)
  42. {
  43. $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), mt_rand(80, 120));
  44. $size = mt_rand(5, Captcha::$config['height'] / 3);
  45. imagefilledellipse($this->image, mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), $size, $size, $color);
  46. }
  47. // Calculate character font-size and spacing
  48. $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / strlen($this->response);
  49. $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response));
  50. // Background alphabetic character attributes
  51. $color_limit = mt_rand(96, 160);
  52. $chars = 'ABEFGJKLPQRTVY';
  53. // Draw each Captcha character with varying attributes
  54. for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++)
  55. {
  56. // Use different fonts if available
  57. $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
  58. $angle = mt_rand(-40, 20);
  59. // Scale the character size on image height
  60. $size = $default_size / 10 * mt_rand(8, 12);
  61. $box = imageftbbox($size, $angle, $font, $this->response[$i]);
  62. // Calculate character starting coordinates
  63. $x = $spacing / 4 + $i * $spacing;
  64. $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
  65. // Draw captcha text character
  66. // Allocate random color, size and rotation attributes to text
  67. $color = imagecolorallocate($this->image, mt_rand(150, 255), mt_rand(200, 255), mt_rand(0, 255));
  68. // Write text character to image
  69. imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]);
  70. // Draw "ghost" alphabetic character
  71. $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));
  72. $char = $chars[mt_rand(0, 14)];
  73. imagettftext($this->image, $size * 2, mt_rand(-45, 45), ($x - (mt_rand(5, 10))), ($y + (mt_rand(5, 10))), $text_color, $font, $char);
  74. }
  75. // Output
  76. return $this->image_render($html);
  77. }
  78. } // End Captcha Alpha Driver Class