首頁  >  文章  >  後端開發  >  php圖片驗證碼函數實現扭曲字元的實現代碼

php圖片驗證碼函數實現扭曲字元的實現代碼

WBOY
WBOY原創
2016-07-25 08:51:391224瀏覽
  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 < 4; $i ){
  21. $text .= $characters{rand(0, $len - 1)};
  22. if(isset($session_name{0}) && session_start()){
  23. $_SESSION[$session_name] = $text;
  24. $_SESSION['CaptchaSessionTime'] = time();
  25. }
  26. }

  27. $font = 'arial.ttf';

  28. $size = floor(0.2 * $width);

  29. $ttfbox = imagettfbbox($size, 0, $font, $text);

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

  32. $array = array(-1, 1);

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

  38. $distortion_im = imagecreatetruecolor ($im_x, $im_y);

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

  49. if($disturb > 0){

  50. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  51. for($i = 0; $i < $disturb; $i ){
  52. imagestring($distortion_im, 3, mt_rand(-10, $im_x), mt_rand(-10, $im_y), $chars[mt_rand(0, 35)], $text_c);
  53. }
  54. }

  55. //加入干扰象素;

  56. if($noise > 0){
  57. for($i = 0; $i < $noise; $i ){
  58. $randcolor = ImageColorallocate($distortion_im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
  59. imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
  60. }
  61. }

  62. // 加干扰曲线

  63. if($curve){
  64. $rand = mt_rand(5, 30);
  65. $rand1 = mt_rand(15, 25);
  66. $rand2 = mt_rand(5, 10);
  67. for ($yy = $rand; $yy <= $rand 2; $yy ){
  68. for ($px = -60; $px <= 60; $px ){
  69. $x = $px / $rand1;
  70. $y = ($x != 0) ? sin($x) : $y;
  71. $py = $y * $rand2;
  72. imagesetpixel($distortion_im, $px 60, $py $yy, $text_c);
  73. }
  74. }
  75. }

  76. //设置文件头;

  77. Header("Content-type: image/JPEG");

  78. //以PNG格式将图像输出到浏览器或文件;

  79. ImagePNG($distortion_im);

  80. //销毁一图像,释放与image关联的内存;

  81. ImageDestroy($distortion_im);
  82. ImageDestroy($im);
  83. }

  84. CaptchaImage('code', 100); //生成一张图片 并将随机数字存放到key为code的session中

复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn