php自訂大小驗證碼的執行個體代碼

來源:互聯網
上載者:User
  1. /**

  2. * php 驗證碼 可自訂大小
  3. * edit bbs.it-home.org
  4. */
  5. function vCode($num=4,$size=20, $width=0,$height=0){
  6. !$width && $width = $num*$size*4/5+5;
  7. !$height && $height = $size + 10;
  8. // 去掉了 0 1 O l 等
  9. $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
  10. $code = '';
  11. for ($i=0; $i<$num; $i++){
  12. $code.= $str[mt_rand(0, strlen($str)-1)];
  13. }
  14. // 畫映像
  15. $im = imagecreatetruecolor($width,$height);
  16. // 定義要用到的顏色
  17. $back_color = imagecolorallocate($im, 235, 236, 237);
  18. $boer_color = imagecolorallocate($im, 118, 151, 199);
  19. $text_color = imagecolorallocate($im, mt_rand(0,200), mt_rand(0,120), mt_rand(0,120));

  20. // 畫背景

  21. imagefilledrectangle($im,0,0,$width,$height,$back_color);
  22. // 畫邊框
  23. imagerectangle($im,0,0,$width-1,$height-1,$boer_color);
  24. // 畫幹擾線
  25. for($i=0;$i<5;$i++){
  26. $font_color = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
  27. imagearc($im,mt_rand(-$width,$width),mt_rand(-$height,$height),mt_rand(30,$width*2),mt_rand(20,$height*2),mt_rand(0,360),mt_rand(0,360),$font_color);
  28. }
  29. // 畫幹擾點
  30. for($i=0;$i<50;$i++){
  31. $font_color = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
  32. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$font_color);
  33. }
  34. // 畫驗證碼
  35. @imagefttext($im, $size , 0, 5, $size+3, $text_color, 'c://WINDOWS//Fonts//simsun.ttc',$code);
  36. header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
  37. header("Content-type: image/png");
  38. imagepng($im);
  39. imagedestroy($im);
  40. }
  41. ?>

複製代碼

調用樣本:

  1. // 驗證碼樣本
  2. //4個字元,大小為20
  3. vCode(4,20);
  4. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.