PHP 變色驗證碼實例

jopen 10年前發布 | 12K 次閱讀 PHP 驗證碼(Captcha)

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); 
// 整理: www.jbxue.com
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); 
?>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!