php 复杂生成验证码图片

  1. function image3($length=4,$type='png',$width=180,$height=60,$font,$verifyName='verify') {
  2. $code = $this->rand_string($length,4);
  3. $width = ($length*25)>$width?$length*25:$width;
  4. $authCode = new Zend_Session_Namespace('Auth_Code');
  5. $authCode->imagecode = $randval;
  6. $im=imagecreatetruecolor($width,$height);
  7. $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
  8. $bkcolor=imagecolorallocate($im,250,250,250);
  9. imagefill($im,0,0,$bkcolor);
  10. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  11. // 干扰
  12. for($i=0;$i<15;$i++){//开源代码phpfensi.com
  13. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  14. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  15. }
  16. for($i=0;$i<255;$i++){
  17. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  18. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  19. }
  20. if(!is_file($fontface)) {
  21. $fontface = dirname(__FILE__)."/".$fontface;
  22. }
  23. for($i=0;$i<$length;$i++){
  24. $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //这样保证随机出来的颜色较深。
  25. $codex= substr($code,$i,1);
  26. imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
  27. }
  28. $this->output($im,$type);
  29. }
  30. function output($im,$type='png')
  31. {
  32. header("Content-type: image/".$type);
  33. $ImageFun='Image'.$type;
  34. $ImageFun($im);
  35. imagedestroy($im);
  36. }