1. 程式人生 > >生成驗證碼---使用畫布

生成驗證碼---使用畫布

文字編碼格式:

<?php
header('content-type:text/html;charset=utf-8');

圖片編碼格式

header(‘content-type:image/png’)
header ( ‘Content-Type: image/gif’ );
header ( ‘Content-Type: image/jpeg’ );

完整程式碼

<?php
//案例:生成驗證碼
header('content-type:image/png');
//編輯一個字串,去掉不容易識別的i,l,o,I,L,O
$str = "abcdefghjkmnpqrstuvwyzABCDEFGHJKMNPQRSTUVWXYZ0123456789";

//畫布
$width = 200;
$height = 100;
$img = imagecreatetruecolor($width,$height);

//顏色
$color = imagecolorallocate($img,0xcc,0xcc,0xcc);

//填充
imagefill($img,0,0,$color);

//畫噪點
for($i=0;$i<100;$i++){
   $color = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
   $x = rand(0,$width);
   $y = rand(0,$height);
   imagesetpixel($img,$x,$y,$color);
}

//畫噪線
for($i=0;$i<30;$i++){
   $color = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
   $x1 = rand(0,$width);
   $y1 = rand(0,$height);
   $x2 = rand(0,$width);
   $y2 = rand(0,$height);
   imageline($img,$x1,$y1,$x2,$y2,$color);
}

//畫文字
$len = strlen($str);
$font = 'simsunb.ttf';
for($i=0;$i<4;$i++){
	$color = imagecolorallocate($img,255,0,0);
    $index = rand(0,$len);
    $chr = substr($str,$index,1);
    $x = 20 + $i * 40;
    $y = 80;
    imagettftext($img,50,rand(-70,70),$x,$y,$color,$font,$chr);
}

//輸出畫布
imagepng($img);

//銷燬畫布
imagedestroy($img);

輸出結果

在這裡插入圖片描述

分析

imagecreatetruecolor
imagecreatetruecolor — 新建一個真彩色影象
說明
resource imagecreatetruecolor ( int $width , int $height );
imagecreatetruecolor() 返回一個影象識別符號,代表了一幅大小為 x_size 和 y_size 的黑色影象

//畫布
$width = 200;
$height = 100;
$img = imagecreatetruecolor($width,$height);

注:寬度,高度可以在()裡設定定值。
也可以在宣告函式前設定變數,在()中使用

imagecolorallocate
imagecolorallocate — 為一幅影象分配顏色
說明
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
imagecolorallocate() 返回一個識別符號,代表了由給定的 RGB 成分組成的顏色。 red , green 和 blue 分別是所需要的顏色的紅,綠,藍成分。這些引數是 0 到 255 的整數或者十六進位制的 0x00 到 0xFF。 imagecolorallocate() 必須被呼叫以建立每一種用在 image 所代表的影象中的顏色。

設定顏色
$color = imagecolorallocate($img,0xcc,0xcc,0xcc);

imagefill
imagefill — 區域填充
說明
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() 在 image 影象的座標 x , y (影象左上角為 0, 0)處用 color 顏色執行區域填充(即與 x, y 點顏色相同且相鄰的點都會被填充)。

//填充
imagefill($img,0,0,$color);

imagesetpixel
imagesetpixel — 畫一個單一畫素 (噪點)
說明
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imagesetpixel() 在 image 影象中用 color 顏色在 x , y 座標(影象左上角為 0,0)上畫一個點。

//畫噪點
for($i=0;$i<100;$i++){
   $color = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
   $x = rand(0,$width);
   $y = rand(0,$height);
   imagesetpixel($img,$x,$y,$color);
}

注:在for迴圈裡設定顏色,xy位置。
用rand隨機函式設定噪點的位置和隨機顏色

imageline
imageline — 畫一條線段
說明
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color 顏色在影象 image 中從座標 x1 , y1 到 x2 , y2 (影象左上角為 0, 0)畫一條線段

//畫噪線
for($i=0;$i<30;$i++){
   $color = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
   $x1 = rand(0,$width);
   $y1 = rand(0,$height);
   $x2 = rand(0,$width);
   $y2 = rand(0,$height);
   imageline($img,$x1,$y1,$x2,$y2,$color);
}

注:在for迴圈裡設定顏色,隨機位置
使用rand函式設定線的顏色,和位置
x1,x2,y1,y2設定線段的起始點

imagettftext
imagettftext — 用 TrueType 字型向影象寫入文字
說明
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
使用 TrueType 字型將 指定的 text 寫入影象。

//畫文字
$len = strlen($str);
$font = 'simsunb.ttf';
for($i=0;$i<4;$i++){
	$color = imagecolorallocate($img,255,0,0);
    $index = rand(0,$len);
    $chr = substr($str,$index,1);
    $x = 20 + $i * 40;
    $y = 80;
    imagettftext($img,50,rand(-70,70),$x,$y,$color,$font,$chr);
}

注:用strlen函式輸出$str的長度
使用substr函式返回相應的位元組數
引用i設定移動文字的位置
simsunb.ttf:在C盤:\Windows檔案\Fonts下,可以將其複製到當前檔案所在的資料夾

imagepng
imagepng — 以 PNG 格式將影象輸出到瀏覽器或檔案
說明
bool imagepng ( resource $image [, string $filename ] )
imagepng() 將 GD 影象流( image )以 PNG 格式輸出到標準輸出(通常為瀏覽器),或者如果用 filename 給出了檔名則將其輸出到該檔案。

//輸出畫布
imagepng($img);

imagedestroy
imagedestroy — 銷燬一影象
說明
bool imagedestroy ( resource $image )
imagedestroy() 釋放與 image 關聯的記憶體。 image 是由影象建立函式返回的影象識別符號,例如 imagecreatetruecolor() 。

//銷燬畫布
imagedestroy($img);