您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)php制作驗(yàn)證碼的過程,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
php使用GD庫生成驗(yàn)證碼
一、繪圖步驟:
1.創(chuàng)建畫布,分配顏色,使用以下兩個(gè)函數(shù)(可以在php手冊GD庫函數(shù)中找到):
imagecreatetruecolor()
imagecolorallocate()
2.繪畫過程:
imagefill()
imagettftext()
imagesetpixel()
imageline()
3.輸出圖像:
header(“Content-Type:image/png”);
imagepng();
imagejpeg();
4.銷毀圖片(釋放內(nèi)存)
imagedestroy();
二、繪圖具體步驟
生成驗(yàn)證碼字符串
private function getCode(){ $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $t=''; for($i=0;$i<$this->m;$i++){ $t.=$str[rand(0,$this->type['letter'])]; } return $t; }
畫出驗(yàn)證碼
function drawCode(){ $code=$this->getCode();//獲取驗(yàn)證碼字符串 imagefill($this->im, 0, 0, $this->bg); for ($i=0; $i <200 ; $i++) { imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255)); } imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255)); $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200)); for($i=0;$i<$this->m;$i++){ imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] ); } }
輸出圖像
function printImage(){ $this->drawCode(); header("Content-Type:image/jpeg"); imagepng($this->im); $this->destroy(); } function destroy(){ imagedestroy($this->im); }
三、代碼演示
<?php /** * 驗(yàn)證碼畫圖類 */ class Image { private $width; private $height; private $im; private $bg; private $m; private $type=array('num' => 9,'letter' => 61 ); function __construct($width,$height,$m) { $this->width=$width; $this->height=$height; $this->m=$m; $this->im=imagecreatetruecolor($this->width, $this->height); $this->bg=imagecolorallocate($this->im, 220, 220, 220); } private function getCode(){ $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $t=''; for($i=0;$i<$this->m;$i++){ $t.=$str[rand(0,$this->type['letter'])]; } return $t; } function drawCode(){ $code=$this->getCode();//獲取驗(yàn)證碼字符串 imagefill($this->im, 0, 0, $this->bg); //加干擾點(diǎn) for ($i=0; $i <200 ; $i++) { imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255)); } //加干擾線 imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255)); $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200)); for($i=0;$i<$this->m;$i++){ imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] ); } } function printImage(){ $this->drawCode(); header("Content-Type:image/jpeg"); imagepng($this->im); $this->destroy(); } function destroy(){ imagedestroy($this->im); } } function unit_test(){ $obj=new Image(20*4,30,4); $obj->printImage(); } unit_test(); ?>
以上就是php制作驗(yàn)證碼的過程,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。