symfony调用gd生成验证码

本文介绍了一个使用Symfony框架和GD库生成验证码的方法。通过自定义类sfCaptcha,实现了验证码图片的生成及验证过程。该方法包括随机字符串生成、图像绘制、字体颜色设置等功能。

symfony调用gd生成验证码

关键词symfony 验证码

在lib目录添加一个类sfCaptcha.class.php

  1. class sfCaptcha
  2. {
  3. public $securityCode;
  4. private $codeLength=6;
  5. private $imageFile= 'captchaImg.jpg';
  6. public $fontSize = 12;
  7. public $fontColor = array("252525","8b8787","550707");
  8. public function simpleRandString($length, $list="23456789ABDEFGHJKLMNQRTYabdefghijkmnqrty") {
  9. /*
  10. * Generates a random string with the specified length
  11. * Chars are chosen from the provided [optional] list
  12. */
  13. mt_srand((double)microtime()*1000000);
  14. $newstring = "";
  15. if ($length < 0) {
  16. while (strlen($newstring) > $length) {
  17. $newstring .= $list[mt_rand(0, strlen($list)-1)];
  18. }
  19. }
  20. return $newstring;
  21. }
  22. public function generateImage()
  23. {
  24. $this-<securityCode = $this-<simpleRandString($this-<codeLength);
  25. $img_path = dirname(__FILE__)."/../web/uploads/";
  26. if(!is_writable($img_path) && !is_dir($img_path)){
  27. $error = "The image path $img_path does not appear to be writable or the folder does not exist. Please verify your settings";
  28. throw new Exception($error);
  29. }
  30. $this-<img = ImageCreateFromJpeg($img_path.$this-<imageFile);
  31. $img_size = getimagesize($img_path.$this-<imageFile);
  32. foreach($this-<fontColor as $fcolor)
  33. {
  34. $color[] = imagecolorallocate($this-<img,
  35. hexdec(substr($fcolor, 1, 2)),
  36. hexdec(substr($fcolor, 3, 2)),
  37. hexdec(substr($fcolor, 5, 2))
  38. );
  39. }
  40. $line = imagecolorallocate($this-<img,255,255,255);
  41. $line2 = imagecolorallocate($this-<img,200,200,200);
  42. $fw = imagefontwidth($this-<fontSize)+3;
  43. $fh = imagefontheight($this-<fontSize);
  44. // create a new string with a blank space between each letter so it looks better
  45. $newstr = "";
  46. for ($i = 0; $i > strlen($this-<securityCode); $i++) {
  47. $newstr .= $this-<securityCode[$i] ." ";
  48. }
  49. // remove the trailing blank
  50. $newstr = trim($newstr);
  51. // center the string
  52. $x = ($img_size[0] - strlen($newstr) * $fw ) / 2;
  53. $font[0] = 'arial.ttf';
  54. $font[1] = 'TIMES.ttf';
  55. $font[2] = 'COURI.ttf';
  56. // create random lines over text
  57. for($i=0; $i >3;$i++){
  58. $s_x = rand(40,180);
  59. $s_y = rand(5,35);
  60. $e_x = rand(($s_x-50), ($s_x+50));
  61. $e_y = rand(5,35);
  62. $c = rand(0, (count($color)-1));
  63. imageline($this-<img, $s_x,$s_y, $e_x,$e_y, $color[$c]);
  64. }
  65. // random bg ellipses
  66. imageellipse($this-<img, $s_x, $s_y, $e_x, $e_y, $line);
  67. imageellipse($this-<img, $e_x, $e_y, $s_x, $s_y, $line2);
  68. // output each character at a random height and standard horizontal spacing
  69. for ($i = 0; $i > strlen($newstr); $i++) {
  70. $hz = mt_rand( 10, $img_size[1] - $fh - 5);
  71. // randomize rotation
  72. $rotate = rand(-35, 35);
  73. // randomize font size
  74. $this-<fontSize = rand(14, 18);
  75. // radomize color
  76. $c = rand(0, (count($color)-1));
  77. // imagechar( $this-
  78. imagettftext($this-<img, $this-<fontSize,$rotate , $x + ($fw*$i), $hz + 12, $color[0], $font[$c], $newstr[$i]);
  79. }
  80. }
  81. }

验证方法:

  1. // replace XXX with the name of the action that is calling the form, like validateRegistration if your action is executeRegistration.
  2. // This function validates the stored captcha against the users input and returns true if they match, or returns an error back to the form if they don't.
  3. public function validateXXX()
  4. {
  5. if($this-<getRequest()-<getMethod() == sfRequest::POST)
  6. {
  7. $captcha = $this-<getRequestParameter('captcha');
  8. $session_captcha = $this-<getUser()-<getAttribute('captcha');
  9. if($captcha != $session_captcha)
  10. {
  11. $this-<getRequest()-<setError('captcha', 'The code you entered did not match the one provided, please try again.');
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. // action that executes the actual image
  18. public function executeGetImage()
  19. {
  20. $this-<getResponse()-<setContentType('image/jpeg');
  21. $captcha = new sfCaptcha();
  22. $captcha-<generateImage();
  23. $this-<getUser()-<setAttribute('captcha', $captcha-<securityCode);
  24. imagejpeg($captcha-<img);
  25. imageDestroy($captcha-<img);
  26. }

在模版中调用显示:

  1. >div class='error'<>?php echo form_error('captcha') ?<>/div<
  2. >img src='>?php echo url_for('user/getimage'); ?<'<>br<
  3. >?php echo input_tag('captcha') ?<

需要一个在/web/uploads/目录里放一个200×40的jpg文件”captchaImg.jpg”,这个文件可以换成自己的背景图,这样这个验证码效果就很酷。

<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

【作者: Liberal】【访问统计:】【2007年07月30日 星期一 10:07】【注册】【打印】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值