php图片验证码函数实现扭曲字符的实现代码

WBOY
Libérer: 2016-07-25 08:51:39
original
1223 Les gens l'ont consulté
  1. /**

  2. * CaptchaImage() - 创建扭曲字符的验证码图片
  3. * $session_name string 验证码图片创建时所需生成Session的变量名
  4. * $width int 验证图片的宽度,默认120,注:图片高度与宽度比例相对固定
  5. * $noise int 干扰素的点数,默认0
  6. * $disturb int 干扰字符个数,默认0
  7. * $curve bool 是否增加干扰曲线,默认ture
  8. * */
  9. function CaptchaImage($session_name = '', $width = 120, $noise = 0, $disturb = 0, $curve = true){
  10. $im_x = $width;
  11. $im_y = ceil(0.25 * $width);
  12. $im = imagecreatetruecolor($im_x, $im_y);
  13. $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
  14. $gray_rand = mt_rand(160, 220);
  15. $buttum_c = ImageColorAllocate($im, $gray_rand, $gray_rand, $gray_rand);
  16. imagefill($im, 0, 0, $buttum_c);
  17. session_start();

  18. $text = '';
  19. $characters = 'ABCEFGHJKLMNPQRSTUVWXYZ';
  20. for($i = 0, $len = strlen($characters); $i $text .= $characters{rand(0, $len - 1)};
  21. if(isset($session_name{0}) && session_start()){
  22. $_SESSION[$session_name] = $text;
  23. $_SESSION['CaptchaSessionTime'] = time();
  24. }
  25. }
  26. $font = 'arial.ttf';

  27. $size = floor(0.2 * $width);
  28. $ttfbox = imagettfbbox($size, 0, $font, $text);

  29. $text_width = abs($ttfbox[2] - $ttfbox[0]);
  30. $side = floor(($im_x - $text_width) / 2);
  31. $array = array(-1, 1);

  32. for ($i = 0; $i $p = array_rand($array);
  33. $an = $array[$p] * mt_rand(5, 10); // 字符倾斜角度
  34. imagettftext($im, $size, $an, $side + $i * ($size - 1), $im_y - 3, $text_c, $font, $text{$i});
  35. }
  36. $distortion_im = imagecreatetruecolor ($im_x, $im_y);

  37. imagefill($distortion_im, 0, 0, $buttum_c);
  38. $distortion = floor(0.04 * $width); // 扭曲程度
  39. for ($i = 0; $i for ($j = 0; $j $rgb = imagecolorat($im, $i , $j);
  40. if( (int)($i + sin($j / $im_y * 2 * M_PI) * $distortion) = 0 ){
  41. imagesetpixel($distortion_im, (int)($i + sin($j / $im_y * 2 * M_PI - M_PI * 0.1) * $distortion) , $j, $rgb);
  42. }
  43. }
  44. }
  45. if($disturb > 0){

  46. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  47. for($i = 0; $i imagestring($distortion_im, 3, mt_rand(-10, $im_x), mt_rand(-10, $im_y), $chars[mt_rand(0, 35)], $text_c);
  48. }
  49. }
  50. //加入干扰象素;

  51. if($noise > 0){
  52. for($i = 0; $i $randcolor = ImageColorallocate($distortion_im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
  53. imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
  54. }
  55. }
  56. // 加干扰曲线

  57. if($curve){
  58. $rand = mt_rand(5, 30);
  59. $rand1 = mt_rand(15, 25);
  60. $rand2 = mt_rand(5, 10);
  61. for ($yy = $rand; $yy for ($px = -60; $px $x = $px / $rand1;
  62. $y = ($x != 0) ? sin($x) : $y;
  63. $py = $y * $rand2;
  64. imagesetpixel($distortion_im, $px + 60, $py + $yy, $text_c);
  65. }
  66. }
  67. }
  68. //设置文件头;

  69. Header("Content-type: image/JPEG");
  70. //以PNG格式将图像输出到浏览器或文件;

  71. ImagePNG($distortion_im);
  72. //销毁一图像,释放与image关联的内存;

  73. ImageDestroy($distortion_im);
  74. ImageDestroy($im);
  75. }
  76. CaptchaImage('code', 100); //生成一张图片 并将随机数字存放到key为code的session中

复制代码


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal