php中文验证码实现示例分享
程序员文章站
2023-12-09 13:24:51
复制代码 代码如下:
<?php
$text = '的一是不了能好都然没日于起还发成事只作当想看文无开手十用主行方又如前所本见经头面公同三已老从动两长知民样现分将外但身些与高意进把法此实回二理美点月明其种声全工己话儿者向情部正名定女问力机给等几很业最间新什打便位因重被走电四第门相次东政海口使教西再平真听世气信北少关并内加化由却代军产入先山五太水万市眼体别处总才场师书比住员九笑性通目华报立马命张活难神数件安表原车白应路期叫死常提感金何更反合放做系计或司利受光王果亲界及今京务制解各任至清物台象记边共风战干接它许八特觉望直服毛林题建南度统色字请交爱让';
$im = imagecreatetruecolor(100,45);
$font = './msyhbd.ttf';
$len = mb_strlen($text,'utf-8');
$grey = imagecolorallocate($im,200,200,200);
imagefill($im,0,0,$grey);
for($i = 0,$x = 0 ;$i < 4 ; $i++ ){
$color = imagecolorallocate($im,mt_rand(0,190),mt_rand(0,190),mt_rand(0,190));
$x += $i*10 + mt_rand(1,5);
$tmp = mb_substr($text,mt_rand(0,$len),1,'utf-8');
imagettftext($im,mt_rand(10,16),mt_rand(-15,15),$x,mt_rand(21,25),$color,$font,$tmp);
}
header('content-type:image/png');
imagepng($im);
imagedestroy($im);
?>
复制代码 代码如下:
<?php
$text = '的一是不了能好都然没日于起还发成事只作当想看文无开手十用主行方又如前所本见经头面公同三已老从动两长知民样现分将外但身些与高意进把法此实回二理美点月明其种声全工己话儿者向情部正名定女问力机给等几很业最间新什打便位因重被走电四第门相次东政海口使教西再平真听世气信北少关并内加化由却代军产入先山五太水万市眼体别处总才场师书比住员九笑性通目华报立马命张活难神数件安表原车白应路期叫死常提感金何更反合放做系计或司利受光王果亲界及今京务制解各任至清物台象记边共风战干接它许八特觉望直服毛林题建南度统色字请交爱让';
$im = imagecreatetruecolor(100,45);
$font = './msyhbd.ttf';
$len = mb_strlen($text,'utf-8');
$grey = imagecolorallocate($im,200,200,200);
imagefill($im,0,0,$grey);
for($i = 0,$x = 0 ;$i < 4 ; $i++ ){
$color = imagecolorallocate($im,mt_rand(0,190),mt_rand(0,190),mt_rand(0,190));
$x += $i*10 + mt_rand(1,5);
$tmp = mb_substr($text,mt_rand(0,$len),1,'utf-8');
imagettftext($im,mt_rand(10,16),mt_rand(-15,15),$x,mt_rand(21,25),$color,$font,$tmp);
}
header('content-type:image/png');
imagepng($im);
imagedestroy($im);
?>