欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

php如何實現(xiàn)圖片驗證碼

這篇文章給大家分享的是有關php如何實現(xiàn)圖片驗證碼的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)專注于安鄉(xiāng)企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設公司,商城系統(tǒng)網(wǎng)站開發(fā)。安鄉(xiāng)網(wǎng)站建設公司,為安鄉(xiāng)等地區(qū)提供建站服務。全流程按需網(wǎng)站開發(fā),專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

php實現(xiàn)圖片驗證碼的方法:1、加載GD擴展;2、創(chuàng)建畫布并在畫布上增加內容;3、通過imagepng保存輸出;4、釋放資源;5、生成隨機驗證碼數(shù)據(jù)即可。

本文操作環(huán)境:Windows7系統(tǒng)、PHP7.1版,DELL G3電腦。

php怎么實現(xiàn)圖片驗證碼?

PHP實現(xiàn)圖片驗證碼功能

驗證碼: captcha, 是一種用于區(qū)別人和電腦的技術
原理(Completely Automated Public Turing Test to Tell Computers and Humans Apart (全自動區(qū)分計算機和人類的圖靈測試)

如何區(qū)分計算機和人類?
只要是文字性的內容,計算機一定可以識別; 計算機是不能識別圖片內容的, 但是人眼非常容易識別圖片中的內容.

驗證碼本質: 將文字性的內容印在圖片上, 用來實現(xiàn)計算機和人類的區(qū)別.

一、圖片擴展了解

PHP本身無法操作圖片.
PHP但是可以利用提供的圖片擴展操作圖片.

圖片擴展有很多: 常用的是GD

php如何實現(xiàn)圖片驗證碼

加載GD擴展: 所有的GD擴展函數(shù)image開頭

php如何實現(xiàn)圖片驗證碼

二、PHP操作圖片

1.增加畫布(創(chuàng)建畫布)

圖片資源 imagecreatetruecolor(寬,高);

2.在畫布上增加內容(文字)

a)給將要在圖片上添加的內容分配顏色: 先將顏色關聯(lián)到圖片資源,然后才可以使用
顏色句柄[整型] imagecolorallocate(圖片資源,紅色,綠色,藍色); //顏色可以使用數(shù)字0-255或者使用十六進制#十六進制

b)寫文字: 只能寫英文(ASCII碼表上的內容)
布爾 imagestring(圖片資源,文字大小,起始X坐標,起始Y坐標,寫內容,顏色);
字體大小: 1-5

3.保存輸出

imagepng(圖片資源[,保存位置]);

4.釋放資源(資源建議釋放)

布爾結果 imagedestroy(圖片資源);

//1.    創(chuàng)建畫布
$img = imagecreatetruecolor(200,200);
//var_dump($img);

//2.    作畫
//2.1   給畫布分配顏色
$color = imagecolorallocate($img,255,255,255);
//echo $color;

//2.2   寫入文字
$true = imagestring($img,5,50,90,'hello world',$color);
//var_dump($true);

//3.    保存輸出內容
//3.1   輸出
//告訴瀏覽器,內容是圖片
//header('Content-type:image/png');
//imagepng($img);

//3.2   保存圖片
imagepng($img,'hello.png'); // 保存到當前目錄

//4.    釋放資源
$res = imagedestroy($img);
var_dump($res);

三、驗證碼圖片

1.生成隨機驗證碼數(shù)據(jù)

2.創(chuàng)建畫布

3.填充背景色: imagefill(圖片資源,起始位置X,起始位置Y,顏色句柄); //imagefill: 找到一個像素點之后, 如果發(fā)現(xiàn)周圍相鄰的像素點與當前像素點顏色一樣(全部黑色)就會被自動渲染.

4.添加文字內容: 先分配顏色

5.保存輸出: 驗證碼都是輸出

6.釋放資源

//制作驗證碼圖片

//獲取驗證碼字符串
$captcha = '';
for($i = 0;$i < 4;$i++){
    //chr: 將數(shù)字轉換成對應的字符(ASCII)
    switch(mt_rand(0,2)){
        case 0: //數(shù)字
            $captcha .= chr(mt_rand(49,57));
            break;
        case 1:    //大寫字母
            $captcha .= chr(mt_rand(65,90));
            break;
        case 2: //小寫字母
            $captcha .= chr(mt_rand(97,122));
            break;
    }
}
//echo $captcha;

//創(chuàng)建畫布
$img = imagecreatetruecolor(200,200);

//給背景分配顏色
$bg = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));

//填充背景色
imagefill($img,0,0,$bg);

//循環(huán)寫入
for($i = 0;$i < 4;$i++){
    //分配文字顏色
    $txt = imagecolorallocate($img,mt_rand(50,150),mt_rand(50,150),mt_rand(50,150));

    //寫入文字
    imagestring($img,mt_rand(1,5),60 + $i*20,90,$captcha[$i],$txt);
}

//輸出
header('Content-type:image/png');
imagepng($img);

//釋放資源
imagedestroy($img);

四、中文驗證碼

兩個注意點
獲取隨機中文: 在PHP中都是以字節(jié)為單位操作數(shù)據(jù),中文在不同字符集中有不同字節(jié)數(shù)
中文寫入函數(shù): imagettftext(圖片資源,字體大小, 字體旋轉角度, 字體的起始X,字體起始Y, 字體文件,內容, 顏色);

1.創(chuàng)建畫布: 填充背景色

2.獲取隨機中文

3.將中文寫入到圖片

4.保存輸出圖片

5.銷毀資源

//中文驗證碼
header('Content-type:text/html;charset=utf-8');

//創(chuàng)建畫布
$img = imagecreatetruecolor(200,200);
//給背景分配顏色
$bg = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
//填充背景色
imagefill($img,0,0,$bg);

//寫入內容
for($i = 0;$i < 4;$i++){
    //分配顏色
    $txt = imagecolorallocate($img,mt_rand(50,150),mt_rand(50,150),mt_rand(50,150));

    //獲取隨機中文
    $string = "今天我寒夜里看雪飄過懷著冷卻了的心窩飄遠方";
    
    $pos = mt_rand(0,strlen($string) - 1);
    $start = $pos - $pos % 3;        //utf-8取模3,GBK取模2

    //取三個長度(字符串截取)
    $target = substr($string,$start,3);

    //寫入中文
    imagettftext($img,mt_rand(20,40),mt_rand(-45,45),40 + $i * 30, mt_rand(80,120),$txt,'simple.ttf',$target);

}

//輸出圖片
header('Content-type:image/png');
imagepng($img);

//銷毀資源
imagedestroy($img);

五、封裝驗證碼類

//驗證碼工具類

class Captcha{
    //屬性
    private $width;
    private $height;
    private $strlen;
    private $lines;     //干擾線數(shù)量
    private $stars;     //干擾點數(shù)量
    private $font;      //字體路徑

    //構造方法:初始化屬性
    public function __construct($info = array()){
        //初始化屬性
        $this->width    = isset($info['width'])?$info['width']:146;
        $this->height    = isset($info['height'])?$info['height']:23;
        $this->strlen    = isset($info['strlen'])?$info['strlen']:4;
        $this->lines    = isset($info['lines'])?$info['lines']:10;
        $this->stars    = isset($info['stars'])?$info['stars']:50;
        $this->font        = isset($info['font'])?$info['font']:'fonts/AxureHandwriting-BoldItalic.otf';
    }

    //生成驗證碼圖片
    public function generate(){
        //創(chuàng)建畫布,給定背景色
        $img = imagecreatetruecolor($this->width,$this->height);
        $c_bg = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
        imagefill($img,0,0,$c_bg);

        //寫入字符串
        $captcha = $this->getStr();
        
        //增加干擾點"*"
        for($i = 0;$i < $this->stars;$i++){
            //隨機顏色
            $c_star = imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));

            //寫入*號
            imagestring($img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$c_star);
        }

        //增加干擾線
        for($i = 0;$i < $this->lines;$i++){
            //隨機顏色
            $c_line = imagecolorallocate($img,mt_rand(150,200),mt_rand(150,200),mt_rand(150,200));

            //劃線
            imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$c_line);
        }

        //隨機顏色
        for($i = 0;$i < $this->strlen;$i++){
            $c_str = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
            imagettftext($img,mt_rand(10,20),mt_rand(-45,45),20 + $i * 30,mt_rand(14,$this->height - 6),$c_str,$this->font,$captcha[$i]);

        }
        
        //輸出圖片
        header('Content-type:image/png');
        imagepng($img);

        //釋放資源
        imagedestroy($img);
    }

    //獲取隨機字符串
    //@return 字符串
    private function getStr(){
        //ASCII碼表生成字符串
        $str = '';

        //循環(huán)生成
        for($i = 0;$i < $this->strlen;$i++){
            //隨機選擇數(shù)字,小寫字母和大寫字母
            switch(mt_rand(0,2)){
                case 0: //數(shù)字
                    $str .= chr(mt_rand(49,57));
                    break;
                case 1: //小寫字母
                    $str .= chr(mt_rand(97,122));
                    break;
                case 2: //大寫字母
                    $str .= chr(mt_rand(65,90));
                    break;
            }
        }

        //將驗證碼字符串保存到session
        $_SESSION['captcha'] = $str;

        //返回結果
        return $str;
    }

    /*
     * 驗證驗證碼
     * @param1 string $captcha,用戶輸入的驗證碼數(shù)據(jù)
     * @return boolean,成功返回true,失敗返回false
     */
    public static function checkCaptcha($captcha){
        //與session中的驗證碼進行驗證

        //驗證碼不區(qū)分大小寫
        return (strtoupper($captcha) === strtoupper($_SESSION['captcha']));
    }

}

感謝各位的閱讀!關于“php如何實現(xiàn)圖片驗證碼”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

分享名稱:php如何實現(xiàn)圖片驗證碼
本文URL:http://chinadenli.net/article32/ihopsc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機建站公司定制開發(fā)品牌網(wǎng)站建設網(wǎng)站設計響應式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化