PHP實現變色驗證碼執行個體

來源:互聯網
上載者:User

 驗證碼想必大家都有見到過吧,在本文為大家介紹下PHP如何?變色驗證碼,感興趣的朋友可以參考下

代碼如下:<?php header("Content-type: image/png,charset='utf-8'"); $im = imagecreatetruecolor(400, 30); //白色 $white = imagecolorallocate($im, 255, 255, 255); //紅色 $red = imagecolorallocate($im, 255, 0, 0); //黑色 $black=imagecolorallocate($im, 0, 0, 0); //綠色 $green=imagecolorallocate($im, 0, 255, 0); //藍色 $blue=imagecolorallocate($im, 0, 0, 255); $color_arr=array($green,$blue,$red); $color=array_rand($color_arr); $text = '我靠這驗證碼太變態啦'; $textlen=iconv_strlen($text,'utf-8');//計算字串長度 //隨機截取兩個字元,變色顯示 $p1=rand(1,$textlen)-1; while(($p2=rand(1,$textlen)-1)==$p1); $w1=iconv_substr($text,$p1,1,'utf-8'); $w2=iconv_substr($text,$p1,1,'utf-8'); //字型檔 (PS:T不錯的php Q扣峮:276167802,驗證:csl) $font = 'simkai.ttf'; imagefilledrectangle($im, 0, 0, 399, 29, $white); for($i=0;$i<$textlen;$i++) { if($i==$p1||$i==$p2) { imagettftext($im, 15, 0, 20*($i-1)+20, 20, $color_arr[$color], $font, iconv_substr($text,$i,1,'utf-8')); } else { imagettftext($im, 15, 0, 20*($i-1)+20, 20, $black, $font, iconv_substr($text,$i,1,'utf-8')); } } imagepng($im); imagedestroy($im); ?>  驗證碼中的字元並不是同一種顏色,讓使用者輸入指定顏色的驗證碼,這樣安全性會更好的。  

聯繫我們

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