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

來源:互聯網
上載者:User
  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中

複製代碼
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.