一漂亮的PHP圖片驗(yàn)證碼實(shí)例
一、顯示效果
二、代碼如下
* @Author fy
*/
$imgwidth =100; //圖片寬度
$imgheight =40; //圖片高度
$codelen =4; //驗(yàn)證碼長度
$fontsize =20; //字體大小
$charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
$font = 'Fonts/segoesc.ttf';
$im=imagecreatetruecolor($imgwidth,$imgheight);
$while=imageColorAllocate($im,255,255,255);
imagefill($im,0,0,$while); //填充圖像
//取得字符串
$authstr='';
$_len = strlen($charset)-1;
for ($i=0;$i<$codelen;$i++) {
$authstr .= $charset[mt_rand(0,$_len)];
}
session_start();
$_SESSION['scode']=strtolower($authstr);//全部轉(zhuǎn)為小寫,主要是為了不區(qū)分大小寫
//隨機(jī)畫點(diǎn),已經(jīng)改為劃星星了
for ($i=0;$i<$imgwidth;$i++){
$randcolor=imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($im,mt_rand(1,5), mt_rand(0,$imgwidth),mt_rand(0,$imgheight), '*',$randcolor);
//imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
}
//隨機(jī)畫線,線條數(shù)量=字符數(shù)量(隨便)
for($i=0;$i<$codelen;$i++)
{
$randcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$randcolor);
}
$_x=intval($imgwidth/$codelen); //計(jì)算字符距離
$_y=intval($imgheight*0.7); //字符顯示在圖片70%的位置
for($i=0;$i<strlen($authstr);$i++){
$randcolor=imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
//imagestring($im,5,$j,5,$imgstr[$i],$color3);
// imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
imagettftext($im,$fontsize,mt_rand(-30,30),$i*$_x+3,$_y,$randcolor,$font,$authstr[$i]);
}
//生成圖像
header("content-type:image/PNG");
imagePNG($im);
imageDestroy($im);
相關(guān)文章
PHP計(jì)算一年多少個(gè)星期和每周的開始和結(jié)束日期
這篇文章主要介紹了PHP計(jì)算每周的開始和結(jié)束日期,php實(shí)現(xiàn)計(jì)算一年多少周,同時(shí)計(jì)算出每一周的開始日期和結(jié)束日期,需要的朋友可以參考下2014-07-07PHP上傳Excel文件導(dǎo)入數(shù)據(jù)到MySQL數(shù)據(jù)庫示例
這篇文章主要介紹了PHP上傳Excel文件導(dǎo)入數(shù)據(jù)到MySQL數(shù)據(jù)庫示例,可以將Excel的數(shù)據(jù)寫入到MySQL數(shù)據(jù)庫中,感興趣的同學(xué)可以了解一下。2016-10-10thinkphp文件處理類Dir.class.php的用法分析
這篇文章主要介紹了thinkphp文件處理類Dir.class.php的用法,以實(shí)例形式分析了基于文件處理類Dir.class.php的自定義函數(shù)del的使用,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12Zend Framework+smarty用法實(shí)例詳解
這篇文章主要介紹了Zend Framework+smarty用法,結(jié)合實(shí)例形式詳細(xì)分析了Zend Framework框架整合Smarty模板的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-03-03