1. 程式人生 > >PHP 使用GD庫 生成驗證碼

PHP 使用GD庫 生成驗證碼

使用函式 phpinfo();gd_info(); 檢視GD 庫相關資訊
如果沒有開啟相關擴充套件 gd_info() 函式將不可用
在 php.ini 配置檔案中 找到 ;extension=php_gd2.dll 去掉前面的分號 儲存並重啟相關服務

利用GD庫 生成驗證碼圖片

截個圖 字型用的tp5 fonts裡面的 6個字型檔案
在這裡插入圖片描述

大致步驟如下:

  • 首先你要有一塊畫布 建立畫布資源 有了畫布就可以隨意畫了
  • 然後給畫布定一個基本的調 ( 背景色填充)
    $img = imagecreatetruecolor($width ,$height);//建立畫布資源
    $color
    = imagecolorallocate($img ,220 ,220 ,220);//建立顏色 imagefill($img ,$color);//填充畫布
  • 接下來是畫一些干擾元素
    • 比如 隨機的 干擾點
    	//$color 是要畫的點的顏色  x  ,y  表示座標  $img 畫布資源
    	imagesetpixel($img, $x, $y, $color);//畫點
    
    • 比如 隨機的 干擾線
    	//$color 是線顏色  x1  ,y1 表示線的開始座標 x2,y2 結束座標 
    	imageline($img, $x1, $y1, $x2, $y2, $color);//畫線
    
    • 比如 隨機的 雪花
    	// $font = 字型大小 1-5  $color 雪花顏色, 莫非不是白色
    	$string = '*';//雪花 ^ ^
    	imagestring($img, $font, $x, $y, $string, $color);//畫一個字元
    
  • 接下來就是最主要的 生成驗證碼文字
    //$img 畫布資源  $size 字型大小  $angle 字型旋轉角度 
    //$fontfile  要使用的字型檔案位置  $text  字元
    imagettftext($img, $size, $angle, $x, $y, $color, $fontfile, $text);//文字寫入畫布
    
  • 最後輸出驗證碼圖片
    header
    ('Content-type :image/png');//圖片格式 .png imagepng($img);//輸出

好了,貼碼 一個灰塵簡單的驗證碼類

<?php
class Verify
{
	protected $string = '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ';
	private   $width;	//驗證碼寬
	private   $height;  //高
	protected $img;		//畫布資源
	protected $code;	//驗證碼文字
	protected $num; 	//驗證碼文字個數
	//protected $config;	//配置項

	public function __construct ($width=160 ,$height=45 ,$num=4)
	{
		$this->width  = $width;
		$this->height = $height;
		$this->num    = $num;
		$this->set_canvas();
		$this->set_disturb();
		$this->set_code();
	}

	//建立畫布資源
	protected function set_canvas ()
	{
		$bgColor = [220,220,220];
		$this->img = imagecreatetruecolor($this->width, $this->height);//畫布資源
		$color 	  = imagecolorallocate($this->img, $bgColor[0], $bgColor[1], $bgColor[2]);
		imagefill($this->img, 0, 0, $color);//填充畫布
	}

	//設定干擾元素
	protected function set_disturb ()
	{
		//if ($this->config['DISTURB_PIXEL']) {//是否開啟點干擾
			//生成干擾元素
			  //隨機點100個
			for ($i=0; $i<100; $i++) {
				$pixel_color = imagecolorallocate($this->img, mt_rand(50,100), mt_rand(50,100), mt_rand(50,100)); //點顏色
				imagesetpixel($this->img, mt_rand(0 ,$this->width-1), mt_rand(0 ,$this->height-1), $pixel_color);//隨機點
			}
		//}
		
		//if ($this->config['DISTURB_SNOW']) {	
			  //生成雪花20個
			for ($i=0; $i<20; $i++) {
				$snow_color = imagecolorallocate($this->img, 255, 255, 255);//雪花顏色
				imagestring($this->img, 3, mt_rand(0 ,$this->width-1), mt_rand(0 ,$this->height-1), '*', $snow_color);//隨機位置雪花
			}
		//}

		//if ($this->config['DISTURB_LINE']) {
			  //干擾線10條
			for ($i=0; $i<10; $i++) {
				$line_color = imagecolorallocate($this->img, mt_rand(50,225), mt_rand(50,225), mt_rand(50,225));//線顏色		
				imageline($this->img, mt_rand(0 ,$this->width-1), mt_rand(0 ,$this->height-1), mt_rand(0,$this->width-1), mt_rand(0 ,$this->height-1), $line_color); //隨機位置線條
			}
		//}
		
	}

	//生成驗證碼文字
	protected function set_code ()
	{
		//生成隨機字元
		$str = '';
		for ($i=0; $i<$this->num; $i++) {
			if (!$this->config['TYPE']) {//程式碼走這裡
				$code = substr($this->string, mt_rand(0 ,strlen($this->string)-1) ,1);//生成隨機字元
			} else { //這裡是中文驗證碼文字  刪掉了 一般用不上
				$code = mb_substr($this->config['DICTIONARY'], mt_rand(0 ,mb_strlen($this->config['DICTIONARY'] ,'utf8')-1) ,1 ,'utf8');
			}
			
			$str .= $code; //拼接字串
			$code_color = imagecolorallocate($this->img, mt_rand(0 ,75), mt_rand(0 ,75), mt_rand(0 ,75));//字型顏色
			$size = mt_rand(20 ,25); //字型大小
			$angle = 0; //旋轉角度
			$x = 10+$i*(($this->width)/$this->num); //字元左下角
			$y = mt_rand($size ,$this->height-5); //字元基本線(非最底部)
			$fontfile = FONT_PATH . mt_rand(1,6) . '.ttf'; //字型檔案位置 自行修改
			imagettftext($this->img, $size, $angle, $x, $y, $code_color, $fontfile, $code);//寫入畫布
		}
		
		$this->code = $str;//儲存文字
	}

	public function show_verify ()
	{
		//輸出驗證碼
		header('Content-type :image/png');
		imagepng($this->img);
	}

	//返回驗證碼文字
	public function get_code ()
	{
		return $this->code;
	}

	function __destruct ()
	{
		imagedestroy($this->img);//銷燬畫布資源
	}
}

還可能用到GD庫的php日常
生成縮圖
圖片新增水印(水印文字, 水印圖片)