本文目錄一覽:
- 1、求助!!在php中想實現圖片驗證碼的效果
- 2、php的驗證碼提示怎樣製作
- 3、求一個php驗證碼及如何使用的例子?
- 4、php圖片驗證碼實現
- 5、UNIX系統下如何實現PHP圖片驗證碼?(200分)
- 6、php的圖片驗證碼代碼
求助!!在php中想實現圖片驗證碼的效果
1.rand一個數串。
2.存入cookie中或session中。
3.將數用gd庫方法生成圖片。
4.用戶輸入後與cookie或session對比。
done
php的驗證碼提示怎樣製作
一般製作驗證碼會按照下面的幾步走:
一:創建出來一個圖片,通常我們成為源,可以用imagecreatetruecolor()這個函數搞定
二:給這個源 添加背景色,同時設置文本顯示的顏色,GD庫函數為我們提供了imagecolorallocate()函數
三:材料弄好了,我們要給它添點內容了,就是我們隨機生成的數字或者字母,甚至可以是它們的組合,這裡我們可以選擇兩個函數 imagettftext()、imagesrting(),這兩個函數的不同,我們會在後面講解。
例:
?php
session_start();//開啟session,用來記錄獲得的驗證碼,這個函數要寫在程序的開頭,不然會出現錯誤
header(“Content-type :image/gif”);//把文件的返回類型設為image/gif格式,這個格式可以輸出圖片
$codelen=4;//設置你要讓用戶輸入字符的個數,一般為4,過長用戶體驗不好。
$charset =”ABCDEFGHKLMNPRSTUVWYZ23456789″;//我們可以盡量把一些難以辨認的字符去掉,比如阿拉伯數字0和字母o,這也是提高用戶體驗的一種方法。
$code =”;
for($i=0;$i$codelen;$i++){//用for循環得到4個隨機的字符,在這裡用到了mt_rand,這個函數比rand的效率要高的多,建議大家用這個
$code .=$charset{mt_rand(0,strlen($charset)-1)};
}
$_SESSION[‘code’]=$code;//下篇關於session驗證的文章將會用到
$width = 80;
$height = 40;
$im = imagecreatetruecolor($width,$height);//用imagecreatetruecolor()函數來建立一個新的圖片,裡面的兩個數值分別是寬度和高度,這是製作驗證碼的第一步
$bg = imagecolorallocate($im,255,255,0); //圖片背景的顏色,這裡是第二步
$textcolor = imagecolorallocate($im,255,0,0);//文字的顏色
imagefill($im,0,0,$bg);//給圖片填充背景色
//好了上面的鋪墊任務做的差不多了,現在關鍵就是讓字符顯示在圖片上,這裡有兩種方法我們一一介紹。
$font =”ggbi.ttf”;//如果你有字體的話 就填上字體的相對路徑,如果沒有就留空。下面的兩個用法我會一一講解。
if($font!==””){
for($num=0;$num4;$num++){
imagettftext($im,mt_rand(12,16),(mt_rand(0,75)+330)%360,5+15*$num,20+mt_rand(2,5),$textcolor,$font,$code[$num]);//這裡是第三步
}
}
else{
for($num=0;$num4;$num++){
imagestring($im,5,10+15*$num,10+mt_rand(0,5),$code[$num],$textcolor);
}
}
header(“Content-type: image/jpeg”);
imagejpeg($im);
?
求一個php驗證碼及如何使用的例子?
以前項目中的東西
在沒有GB庫的情況下也可以生成驗證碼
關鍵部分做出了注釋
img src=”image.php”
?php
//存為文件 image.php
error_reporting(E_ALL ^ E_NOTICE);
require_once(‘image.class.php’);
$image_model = new Image();
//可以在此處更改屬性達到驗證碼不同大小的圖片
$image_model-outputBrowser();
?
?php
/*
* \驗證碼生成類 存為文件image.class.php
* \如果不支持GD庫,則採用像素畫法
* \詳見
*/
class Image
{
var $img_height = 20; //圖片的長
var $img_width = 68; //圖片的寬
var $img_cache = ”; //圖像信息緩存
var $check_number = ”; //驗證碼
var $lenght = 4; //驗證碼位數
var $is_gd = false;
//’———-以下為不支持GD需要的變量
var $pixels = ”;
var $digits = ”;
var $lines = ”;
var $data = ”;
var $chunk = ”;
var $crc_table = ”;
function Image()
{
session_start();
session_register(“check_number”);
$this-session = $_SESSION;
$this-is_gd = function_exists(‘gd_info’) ? true : false;
$this-getCheckNumber();
if( $this-is_gd ):
$this-getFromGD();
else:
$this-getFromCode();
endif;
}
//’—-瀏覽器輸出
function outputBrowser()
{
if( $this-is_gd )
{
Header(“Content-type: image/png”); //告訴瀏覽器,下面的數據是圖片,而不要按文字顯示
Imagepng($this-img_cache); //生成png格式。。。嘿嘿效果蠻像回事的嘛。。。
ImageDestroy($this-img_cache);
}
else echo($this-img_cache);
}
//’—-文件輸出
function outputFile($filename=null)
{
if(!$filename)$filename=time().’.png’;
$fp = fopen($filename, ‘w+’);
fwrite($fp, $this-img_cache);
fclose($fp);
}
//’—-得到驗證碼
function getCheckNumber()
{
//srand(microtime() * 100000);//PHP420後,srand不是必須的
for($Tmpa=0;$Tmpa$this-lenght;$Tmpa++)
{
$this-check_number.= $this-is_gd ? dechex(rand(0,15)) : rand(0,3);
}
unset($Tmpa);
$this-session[‘check_number’] = strtoupper($this-check_number);
}
//’—-用GD庫生成
function getFromGD()
{
//$black = ”;
$this-img_cache = imageCreate($this-img_width,$this-img_height); //生成圖片
//圖片底色,ImageColorAllocate第1次定義顏色PHP就認為是底色了
$black = ImageColorAllocate($this-img_cache, 255,255,255);
//下面該生成雪花背景了,其實就是在圖片上生成一些符號
//先用100個做測試
/*
for ($i=1; $i=15; $i++)
{
imageString($this-img_cache,1,mt_rand(1,$this-img_height-10),mt_rand(1,$this-img_width-10),”.”,
imageColorAllocate($this-img_cache,mt_rand(100,255),mt_rand(0,250),mt_rand(0,200)));
//哈,看到了吧,其實也不是雪花,就是生成*號而已。為了使它們看起來”雜亂無章、5顏6色”,就得在1個1個生成它們的時候,讓它們的位置、顏色,甚至大小都用隨機數,rand()或mt_rand都可以完成。
}
*/
//上面生成了背景,現在就該把已經生成的隨機數放上來了。道理和上面差不多,隨機數1個1個地放,同時讓他們的位置、大小、顏色都用成隨機數~~
//為了區別於背景,這裡的顏色不超過200,上面的不小於200
imagettftext($this-img_cache, 20, 0, 10, 20, $black, “./html/VINERITC.TTF”, “AC67”);
for ($i=0;$istrlen($this-session[‘check_number’]);$i++)
{
imagettftext($this-img_cache, 14, rand(0,30), $i*$this-img_width/4 + 3, 17, imageColorAllocate($this-img_cache,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)), “./html/VINERITC.TTF”, $this-session[‘check_number’][$i]);
}
for($i=0;$i100;$i++)//加入干擾象素
{
imagesetpixel($this-img_cache, rand()%70 , rand()%30 , imageColorAllocate($this-img_cache,mt_rand(100,255),mt_rand(0,250),mt_rand(0,200)));
}
$black = ImageColorAllocate($this-img_cache, 0,0,0); //定義需要的黑色
//先成一黑色的矩形把圖片包圍
ImageRectangle($this-img_cache,0,0, $this-img_width-1,$this-img_height-1, $black);
}
//End Function getGD
#———————————————————————————————————————–
function set_4pixel($r, $g, $b, $x, $y)
{
$ofs = 3 * ($this-img_width * $y + $x);
$this-pixels[$ofs] = chr($r);
$this-pixels[$ofs + 1] = chr($g);
$this-pixels[$ofs + 2] = chr($b);
$this-pixels[$ofs + 3] = chr($r);
$this-pixels[$ofs + 4] = chr($g);
$this-pixels[$ofs + 5] = chr($b);
$ofs += 3 * $this-img_width;
$this-pixels[$ofs] = chr($r);
$this-pixels[$ofs + 1] = chr($g);
$this-pixels[$ofs + 2] = chr($b);
$this-pixels[$ofs + 3] = chr($r);
$this-pixels[$ofs + 4] = chr($g);
$this-pixels[$ofs + 5] = chr($b);
}
//生成數字圖象的函數
function draw2digits($x, $y, $number)
{
//$this-draw_digit($x, $y, intval($number / 10) );
$this-draw_digit($x + 11, $y, $number % 10);
}
function draw_digit($x, $y, $digit)
{
$digit = $this-digits[$digit];
$m = 8;
for($b = 1, $i = 0; $i 7; $i++, $b *= 2)
{
if(($b $digit) == $b)
{
$j = $i * 4;
$x0 = $this-lines[$j] * $m + $x;
$y0 = $this-lines[$j + 1] * $m + $y;
$x1 = $this-lines[$j + 2] * $m + $x;
$y1 = $this-lines[$j + 3] * $m + $y;
if($x0 == $x1)
{
$ofs = 3 * ($this-img_width * $y0 + $x0);
for($h = $y0; $h = $y1; $h++, $ofs += 3 * $this-img_width)
{
$this-pixels[$ofs] = chr(0);
$this-pixels[$ofs + 1] = chr(0);
$this-pixels[$ofs + 2] = chr(0);
}
}
else
{
$ofs = 3 * ($this-img_width * $y0 + $x0);
for($w = $x0; $w = $x1; $w++)
{
$this-pixels[$ofs++] = chr(0);
$this-pixels[$ofs++] = chr(0);
$this-pixels[$ofs++] = chr(0);
}
}
}//End if
}//End for
}
//將文字加入到圖象中
function add_chunk($type)
{
// chunk :為層
// length: 4 字節: 用來計算 chunk
// chunk type: 4 字節
// chunk data: length bytes
// CRC: 4 字節: 循環冗餘碼校驗
// copy data and create CRC checksum
$len = strlen($this-data);
$this-chunk = pack(“c*”, ($len 24) 255,
($len 16) 255,
($len 8) 255,
$len 255);
$this-chunk .= $type;
$this-chunk .= $this-data;
//calculate a CRC checksum with the bytes chunk[4..len-1]
$z = 16777215;
$z |= 255 24;
$c = $z;
for ($n = 4; $n strlen($this-chunk); $n++) {
$c8 = ($c 8) 0xffffff;
$c = $this-crc_table[($c ^ ord($this-chunk[$n])) 0xff] ^ $c8;
}
$crc = $c ^ $z;
$this-chunk .= chr(($crc 24) 255);
$this-chunk .= chr(($crc 16) 255);
$this-chunk .= chr(($crc 8) 255);
$this-chunk .= chr($crc 255);
//將結果加到$img_cache中
$this-img_cache .= $this-chunk;
}
//主程序
function getFromCode()
{
//填充
for($h = 0; $h $this-img_height; $h++)
{
for($w = 0; $w $this-img_width; $w++)
{
$r = 100 / $this-img_width * $w + 155;
$g = 100 / $this-img_height * $h + 155;
$b = 255 – (100 / ($this-img_width + $this-img_height) * ($w + $h));
$this-pixels .= chr($r);
$this-pixels .= chr($g);
$this-pixels .= chr($b);
}
}
$this-digits = array(95, 5, 118, 117, 45, 121, 123, 69, 127, 125);
$this-lines = array(1, 1, 1, 2, 0, 1, 0, 2, 1, 0, 1, 1, 0, 0, 0, 1, 0, 2, 1, 2, 0, 1, 1, 1, 0, 0, 1, 0);
for($i=0; $i$this-lenght; $i++)
{
$this-draw2digits($i*13, 2, $this-check_number[$i]);
}
//$this-set_4pixel(0, 0, 0, 26, 7);
//$this-set_4pixel(0, 0, 0, 26, 13);
//$this-set_4pixel(0, 0, 0, 52, 7);
//$this-set_4pixel(0, 0, 0, 52, 13);
//創建循環冗餘碼校驗表
$z = -306674912;// = 0xedb88320
for ($n = 0; $n 256; $n++)
{
$c = $n;
for ($k = 0; $k 8; $k++)
{
$c2 = ($c 1) 0x7fffffff;
if($c 1) $c = $z ^ ($c2);
else $c = $c2;
}
$this-crc_table[$n] = $c;
}
// PNG file signature
$this-img_cache = pack(“c*”, 137,80,78,71,13,10,26,10);
// IHDR chunk data:
// width: 4 bytes
// height: 4 bytes
// bit depth: 1 byte (8 bits per RGB value)
// color type: 1 byte (2 = RGB)
// compression method: 1 byte (0 = deflate/inflate)
// filter method: 1 byte (0 = adaptive filtering)
// interlace method: 1 byte (0 = no interlace)
$this-data = pack(“c*”, ($this-img_width24) 255,
($this-img_width 16) 255,
($this-img_width 8) 255,
$this-img_width 255,
($this-img_height 24) 255,
($this-img_height 16) 255,
($this-img_height 8) 255,
$this-img_height 255, 8, 2, 0, 0, 0);
$this-add_chunk(“IHDR”);
//以下不敢亂翻譯,請自行參考
// scanline:
// filter byte: 0 = none
// RGB bytes for the line
// the scanline is compressed with “zlib”, method 8 (RFC-1950):
// compression method/flags code: 1 byte ($78 = method 8, 32k window)
// additional flags/check bits: 1 byte ($01: FCHECK = 1, FDICT = 0, FLEVEL = 0)
// compressed data blocks: n bytes
// one block (RFC-1951):
// bit 0: BFINAL: 1 for the last block
// bit 1 and 2: BTYPE: 0 for no compression
// next 2 bytes: LEN (LSB first)
// next 2 bytes: one’s complement of LEN
// LEN bytes uncompressed data
// check value: 4 bytes (Adler-32 checksum of the uncompressed data)
$len = ($this-img_width * 3 + 1) * $this-img_height;
$this-data = pack(“c*”, 0x78, 0x01, 1, $len 255, ($len8) 255, 255 – ($len 255), 255 – (($len 8) 255) );
$start = strlen($this-data);
$i2 = 0;
for($h = 0; $h $this-img_height; $h++)
{
$this-data .= chr(0);
for($w = 0; $w $this-img_width * 3; $w++)
{
$this-data .= $this-pixels[$i2++];
}
}
//calculate a Adler32 checksum with the bytes data[start..len-1]
$s1 = 1;
$s2 = 0;
for ($n = $start; $n strlen($this-data); $n++)
{
$s1 = ($s1 + ord($this-data[$n])) % 65521;
$s2 = ($s2 + $s1) % 65521;
}
$adler = ($s2 16) | $s1;
$this-data .= chr(($adler 24) 255);
$this-data .= chr(($adler 16) 255);
$this-data .= chr(($adler 8) 255);
$this-data .= chr($adler 255);
$this-add_chunk(“IDAT”);
//IEND: marks the end of the PNG-file
$this-data = “”;
$this-add_chunk(“IEND”);
//列印圖象
//echo($this-img_cache);
}
}//End Class
?
php圖片驗證碼實現
可以用php的GD庫做
//隨機生成驗證碼
class randomString
{
function createRandomStr($strLen)
{
list($usec, $sec) = explode(‘ ‘, microtime());
(float) $sec + ((float) $usec * 100000);
$number = ”;
$number_len = $strLen;
$stuff = ‘1234567890abcdefghijklmnopqrstuvwxyz’;//附加碼顯示範圍ABCDEFGHIJKLMNOPQRSTUVWXYZ
$stuff_len = strlen($stuff) – 1;
for ($i = 0; $i $number_len; $i++) {
$number .= substr($stuff, mt_rand(0, $stuff_len), 1);
}
return $number;
}
}
通過ZD庫將驗證碼變成圖片
$number = $createStr-createRandomStr(‘4’);//驗證碼的位數
$number_len = strlen($number);
$_SESSION[“VERIFY_CODE”] = $number;
// 生成驗證碼圖片
$img_width = 60;
$img_height = 20;
$img = imageCreate($img_width, $img_height);
ImageColorAllocate($img, 0x6C, 0x74, 0x70);
$white = ImageColorAllocate($img, 0xff, 0xff, 0xff);
$ix = 6;
$iy = 2;
for ($i = 0; $i $number_len; $i++) {
imageString($img, 5, $ix, $iy, $number[$i], $white);
$ix += 14;
}
for($i=0;$i200;$i++) //加入干擾象素
{
$randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($img, rand()%100 , rand()%50 , $randcolor);
}
// 輸出圖片
header(“Content-type: ” . image_type_to_mime_type(IMAGETYPE_PNG));
imagepng($img);
imagedestroy($img);
UNIX系統下如何實現PHP圖片驗證碼?(200分)
UNIX和Windows生成驗證碼的方式基本上是一樣的,會影響實現方式的主要是PHP和GD的版本,所以如果條件允許的話,最好安裝最新版本的,否則,一些函數會出現不能夠支持的現象。
以下是一些實現的思路,供你參考:
在這個基礎上,還可以做很多變化,以抵抗OCR。
調用頁面:file1.php
?php
session_start();
echo $_SESSION[‘validcode’]; //這個就是上次發送的驗證碼
?
img src=”create.php” border=”1″ /
生成頁面,可以把它看作一個圖片文件:file2.php
?php
session_start();
Header(“content-type: image/png”);
srand((double)microtime()*1000000);
$randval = substr(rand(), 0,4);
$font = 4;
$im = imagecreate(46,16);
$background_color = ImageColorAllocate($im,255,255,255);
$foreground_color = ImageColorAllocate($im,0,0,0);
imagefill($im,0,0,$background_color);
$px = (imagesx($im)-8.3*strlen($randval))/2;
ImageString($im,$font,1,1,$randval,$foreground_color);
Imagepng($im);
ImageDestroy($im);
$_SESSION[‘validcode’] = $randval; //把它保存在會話里, 用來驗證
?
UNIX下GD的安裝,只要在編譯PHP時加入 –with-gd[=DIR] 就可以了。
詳細的安裝方法可以參考:
php的圖片驗證碼代碼
這個是phpcms的驗證碼,經過十幾萬個網站經驗的,非常好用
?php
session_start();
$enablegd = 1;
//判斷圖像處理函數是否存在
$funcs = array(‘imagecreatetruecolor’,’imagecolorallocate’,’imagefill’,’imagestring’,’imageline’,’imagerotate’,’imagedestroy’,’imagecolorallocatealpha’,’imageellipse’,’imagepng’);
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}
ob_clean(); //清理緩衝
if($enablegd)
{
//create captcha
$consts = ‘cdfgkmnpqrstwxyz23456’;
$vowels = ‘aek23456789’;
for ($x = 0; $x 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //獲取$consts中的一個隨機數
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //獲取$vowels中的一個隨機數
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION[‘checkcode’] = $string = substr($radomstring,0,4); //顯示4個字符
$imageX = strlen($radomstring)*8; //圖像的寬
$imageY = 20; //圖像的高
$im = imagecreatetruecolor($imageX,$imageY); //新建一個真彩色圖像
//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
);
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配顏色並說明透明度
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中間背景
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中間背景2
//與左上角的顏色相同的都會被填充
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//往圖像上寫入文字
imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/ALGER.TTF’, $string[0]);
imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/ARIALNI.TTF’, $string[1]);
imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/ALGER.TTF’, $string[2]);
imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.’include/fonts/arial.ttf’, $string[3]);
//畫邊框
$border = imagecolorallocate($im, 133, 153, 193);
imagerectangle($im, 0, 0, $imageX – 1, $imageY – 1, $border);
//畫一些隨機出現的點
$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i80;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//畫隨機出現的線
for ($x=0; $x9;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //畫線
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //畫橢圓
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header(“content-type:image/png\r\n”);
imagepng($im);
imagedestroy($im);
}
else
{
$files = glob(XINCHENG_ROOT.’images/checkcode/*.jpg’);
if(!is_array($files)) die(‘請檢查文件目錄完整性:/images/checkcode/’);
$checkcodefile = $files[rand(0, count($files)-1)]; //隨機其中一個文件
$_SESSION[‘checkcode’] = substr(basename($checkcodefile), 0, 4); //獲得文件名
header(“content-type:image/jpeg\r\n”);
include $checkcodefile;
}
?
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/160604.html