PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/extensions/CdCaptcha/CdCaptchaAction.php

https://github.com/smarteng/onenoteme
PHP | 53 lines | 47 code | 1 blank | 5 comment | 6 complexity | e701a7d752cf030edefc71e85e05b887 MD5 | raw file
  1. <?php
  2. class CdCaptchaAction extends CCaptchaAction
  3. {
  4. protected function generateVerifyCode()
  5. {
  6. if ($this->minLength < 3)
  7. $this->minLength = 3;
  8. if ($this->maxLength > 20)
  9. $this->maxLength = 20;
  10. if ($this->minLength > $this->maxLength)
  11. $this->maxLength = $this->minLength;
  12. $length = rand($this->minLength, $this->maxLength);
  13. $letters = '0123456789';
  14. for ($i = 0; $i < $length; ++ $i) {
  15. $code .= $letters[mt_rand(0, 9)];
  16. }
  17. return $code;
  18. }
  19. /**
  20. * Renders the CAPTCHA image based on the code.
  21. * @param string the verification code
  22. * @return string image content
  23. */
  24. protected function renderImage($code)
  25. {
  26. $image = imagecreatetruecolor($this->width, $this->height);
  27. $backColor = imagecolorallocate($image, (int) ($this->backColor % 0x1000000 / 0x10000), (int) ($this->backColor % 0x10000 / 0x100), $this->backColor % 0x100);
  28. imagefilledrectangle($image, 0, 0, $this->width, $this->height, $backColor);
  29. imagecolordeallocate($image, $backColor);
  30. $foreColor = imagecolorallocate($image, (int) ($this->foreColor % 0x1000000 / 0x10000), (int) ($this->foreColor % 0x10000 / 0x100), $this->foreColor % 0x100);
  31. if ($this->fontFile === null)
  32. $this->fontFile = dirname(__FILE__) . '/Duality.ttf';
  33. $len = strlen($code);
  34. $fontSize = $this->height - $this->padding * 2;
  35. for ($i = 0; $i < $len; $i ++) {
  36. $foreColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  37. $angle = mt_rand(- 20, 20);
  38. $x = 5 + $fontSize * $i;
  39. $y = $this->height;
  40. imagettftext($image, $fontSize, $angle, $x, $y, $foreColor, $this->fontFile, $code[$i]);
  41. imageline($image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $foreColor);
  42. }
  43. imagecolordeallocate($image, $foreColor);
  44. header('Pragma: public');
  45. header('Expires: 0');
  46. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  47. header('Content-Transfer-Encoding: binary');
  48. header("Content-type: image/png");
  49. imagepng($image);
  50. imagedestroy($image);
  51. }
  52. }