一漂亮的PHP圖片驗證碼執行個體_PHP教程

來源:互聯網
上載者:User
一、顯示效果

二、代碼如下
複製代碼 代碼如下:/*
* @Author fy
*/

$imgwidth =100; //圖片寬度
$imgheight =40; //圖片高度
$codelen =4; //驗證碼長度
$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);//全部轉為小寫,主要是為了不區分大小寫

//隨機畫點,已經改為劃星星了
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);
}
//隨機畫線,線條數量=字元數量(隨便)
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); //計算字元距離
$_y=intval($imgheight*0.7); //字元顯示在圖片70%的位置
for($i=0;$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);


http://www.bkjia.com/PHPjc/744335.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/744335.htmlTechArticle一、顯示效果 二、代碼如下 複製代碼 代碼如下: /* * @Author fy */ $imgwidth=100; //圖片寬度 $imgheight=40; //圖片高度 $codelen=4; //驗證碼長度 $fonts...

  • 聯繫我們

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