Implementation code for php image verification code function to realize distorted characters

WBOY
Release: 2016-07-25 08:51:39
Original
1216 people have browsed it
  1. /**

  2. * CaptchaImage() - Create a verification code image with distorted characters
  3. * $session_name string The variable name of the Session that needs to be generated when creating the verification code image
  4. * $width int The width of the verification image, default 120, Note: The ratio of image height to width Relatively fixed
  5. * $noise int The number of interferon points, the default is 0
  6. * $disturb int The number of interference characters, the default is 0
  7. * $curve bool Whether to increase the interference curve, the default is true
  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中

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template