php生成圖形驗(yàn)證碼幾種方法小結(jié)
更新時(shí)間:2013年08月15日 15:37:18 作者:
生成圖形驗(yàn)證碼需要使用php GD庫(kù)來(lái)生成,如果你沒(méi)開(kāi)戶GD庫(kù)我們需要在php.ini文件找到extension=php_gd2.dll 去掉前面的;就行了,然后重啟apache 或iis環(huán)境即可
我們先來(lái)檢查一下自己的php是不是打開(kāi)了gd庫(kù)。
<?php
if(extension_loaded('gd')) {
echo '你可以使用gd<br>';
foreach(gd_info() as $cate=>$value)
echo "$cate: $value<br>";
}else
echo '你沒(méi)有安裝gd擴(kuò)展';
?>
如果有返回信息就正確可以常用使用了
例1
<?php
/**
* vCode(m,n,x,y) m個(gè)數(shù)字 顯示大小為n 邊寬x 邊高y
* 自己改寫(xiě)記錄session $code
*/
session_start();
vCode(4, 15); //4個(gè)數(shù)字,顯示大小為15
function vCode($num = 4, $size = 20, $width = 0, $height = 0) {
!$width && $width = $num * $size * 4 / 5 + 5;
!$height && $height = $size + 10;
// 去掉了 0 1 O l 等
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
$code = '';
for ($i = 0; $i < $num; $i++) {
$code .= $str[mt_rand(0, strlen($str)-1)];
}
// 畫(huà)圖像
$im = imagecreatetruecolor($width, $height);
// 定義要用到的顏色
$back_color = imagecolorallocate($im, 235, 236, 237);
$boer_color = imagecolorallocate($im, 118, 151, 199);
$text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
// 畫(huà)背景
imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
// 畫(huà)邊框
imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color);
// 畫(huà)干擾線
for($i = 0;$i < 5;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
}
// 畫(huà)干擾點(diǎn)
for($i = 0;$i < 50;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
}
// 畫(huà)驗(yàn)證碼
@imagefttext($im, $size , 0, 5, $size + 3, $text_color, 'c:\WINDOWS\Fonts\simsun.ttc', $code);
$_SESSION["VerifyCode"]=$code;
header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
header("Content-type: image/png;charset=gb2312");
imagepng($im);
imagedestroy($im);
}
?>
例2
使用PHP,結(jié)合session和GD庫(kù)擴(kuò)展開(kāi)發(fā)的一個(gè)生成驗(yàn)證碼的例子(w3c推薦),可以很方便的用于項(xiàng)目中。而且樣式美觀
//首先開(kāi)啟session
session_start();
//定義前臺(tái)顯示驗(yàn)證碼長(zhǎng)&寬
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 10;
$random_lines = 30;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
//定義要生成驗(yàn)證碼的字符串
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
//設(shè)置session,做驗(yàn)證
$_SESSION['6_letters_code'] = $code;
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
個(gè)人推薦推薦第二個(gè)生成驗(yàn)證碼程序代碼,各位同學(xué)可嘗試參考對(duì)比哦,最后一個(gè)是W3C標(biāo)準(zhǔn)生成的也是利用了php gd庫(kù)。
復(fù)制代碼 代碼如下:
<?php
if(extension_loaded('gd')) {
echo '你可以使用gd<br>';
foreach(gd_info() as $cate=>$value)
echo "$cate: $value<br>";
}else
echo '你沒(méi)有安裝gd擴(kuò)展';
?>
如果有返回信息就正確可以常用使用了
例1
復(fù)制代碼 代碼如下:
<?php
/**
* vCode(m,n,x,y) m個(gè)數(shù)字 顯示大小為n 邊寬x 邊高y
* 自己改寫(xiě)記錄session $code
*/
session_start();
vCode(4, 15); //4個(gè)數(shù)字,顯示大小為15
function vCode($num = 4, $size = 20, $width = 0, $height = 0) {
!$width && $width = $num * $size * 4 / 5 + 5;
!$height && $height = $size + 10;
// 去掉了 0 1 O l 等
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
$code = '';
for ($i = 0; $i < $num; $i++) {
$code .= $str[mt_rand(0, strlen($str)-1)];
}
// 畫(huà)圖像
$im = imagecreatetruecolor($width, $height);
// 定義要用到的顏色
$back_color = imagecolorallocate($im, 235, 236, 237);
$boer_color = imagecolorallocate($im, 118, 151, 199);
$text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
// 畫(huà)背景
imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
// 畫(huà)邊框
imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color);
// 畫(huà)干擾線
for($i = 0;$i < 5;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
}
// 畫(huà)干擾點(diǎn)
for($i = 0;$i < 50;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
}
// 畫(huà)驗(yàn)證碼
@imagefttext($im, $size , 0, 5, $size + 3, $text_color, 'c:\WINDOWS\Fonts\simsun.ttc', $code);
$_SESSION["VerifyCode"]=$code;
header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
header("Content-type: image/png;charset=gb2312");
imagepng($im);
imagedestroy($im);
}
?>
例2
使用PHP,結(jié)合session和GD庫(kù)擴(kuò)展開(kāi)發(fā)的一個(gè)生成驗(yàn)證碼的例子(w3c推薦),可以很方便的用于項(xiàng)目中。而且樣式美觀
復(fù)制代碼 代碼如下:
//首先開(kāi)啟session
session_start();
//定義前臺(tái)顯示驗(yàn)證碼長(zhǎng)&寬
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = './monofont.ttf';
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = '23456789bcdfghjkmnpqrstvwxyz';
$random_dots = 10;
$random_lines = 30;
$captcha_text_color="0x142864";
$captcha_noice_color = "0x142864";
//定義要生成驗(yàn)證碼的字符串
$code = '';
$i = 0;
while ($i < $characters_on_image) {
$code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color['red'],
$arr_text_color['green'], $arr_text_color['blue']);
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image, $arr_noice_color['red'],
$arr_noice_color['green'], $arr_noice_color['blue']);
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image, mt_rand(0,$image_width),
mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height),
mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color);
}
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code);
$x = ($image_width - $textbox[4])/2;
$y = ($image_height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code);
/* Show captcha image in the page html page */
header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
//設(shè)置session,做驗(yàn)證
$_SESSION['6_letters_code'] = $code;
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
個(gè)人推薦推薦第二個(gè)生成驗(yàn)證碼程序代碼,各位同學(xué)可嘗試參考對(duì)比哦,最后一個(gè)是W3C標(biāo)準(zhǔn)生成的也是利用了php gd庫(kù)。
您可能感興趣的文章:
- thinkphp整合系列之極驗(yàn)滑動(dòng)驗(yàn)證碼geetest功能
- Ajax和PHP正則表達(dá)式驗(yàn)證表單及驗(yàn)證碼
- PHP實(shí)現(xiàn)登陸表單提交CSRF及驗(yàn)證碼
- Ajax提交表單時(shí)驗(yàn)證碼自動(dòng)驗(yàn)證 php后端驗(yàn)證碼檢測(cè)
- php生成圖片驗(yàn)證碼-附五種驗(yàn)證碼
- php 生成隨機(jī)驗(yàn)證碼圖片代碼
- PHP制作圖形驗(yàn)證碼代碼分享
- PHP5中GD庫(kù)生成圖形驗(yàn)證碼(有漢字)
- PHP生成Gif圖片驗(yàn)證碼
- 如何用php生成扭曲及旋轉(zhuǎn)的驗(yàn)證碼圖片
- php+js實(shí)現(xiàn)的拖動(dòng)滑塊驗(yàn)證碼驗(yàn)證表單操作示例【附源碼下載】
相關(guān)文章
php操作JSON格式數(shù)據(jù)的實(shí)現(xiàn)代碼
php操作JSON格式數(shù)據(jù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12php數(shù)組保存文本與文本反編成數(shù)組實(shí)例
這篇文章主要介紹了php數(shù)組保存文本與文本反編成數(shù)組的方法,通過(guò)兩個(gè)自定義函數(shù)string2array與array2string實(shí)例展示了php數(shù)組保存文本與文本反編成數(shù)組的實(shí)現(xiàn)方法,具有不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11php實(shí)現(xiàn)refresh刷新頁(yè)面批量導(dǎo)入數(shù)據(jù)的方法
這篇文章主要介紹了php實(shí)現(xiàn)refresh刷新頁(yè)面批量導(dǎo)入數(shù)據(jù)的方法,借鑒了dedecms批量生成html的原理,采用refresh代替js跳轉(zhuǎn)來(lái)實(shí)現(xiàn),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12PHP實(shí)現(xiàn)創(chuàng)建一個(gè)RPC服務(wù)操作示例
這篇文章主要介紹了PHP實(shí)現(xiàn)創(chuàng)建一個(gè)RPC服務(wù)操作,結(jié)合實(shí)例形式分析了PHP基于socket擴(kuò)展通信實(shí)現(xiàn)的RPC遠(yuǎn)程調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2020-02-02php實(shí)現(xiàn)數(shù)字補(bǔ)零的方法總結(jié)
這篇文章給大家總結(jié)了關(guān)于php實(shí)現(xiàn)數(shù)字補(bǔ)零的方法以及相關(guān)代碼分享,有興趣的朋友們學(xué)習(xí)下。2018-09-09phpMyAdmin 鏈接表的附加功能尚未激活的問(wèn)題
安裝了phpMyAdmin之后一直有這個(gè)問(wèn)題。雖然知道自己可能用不到這些功能,但是不解決這個(gè)問(wèn)題總覺(jué)著phpMyAdmin和MySQL的安裝沒(méi)有成功。2010-08-08