導航:首頁 > 編程語言 > php圖片驗證碼識別

php圖片驗證碼識別

發布時間:2022-11-17 06:10:18

php圖片驗證碼實現

可以用php的GD庫做

//隨機生成驗證碼
class randomString
{

function createRandomStr($strLen)
{
list($usec, $sec) = explode(' ', microtime());
(float) $sec + ((float) $usec * 100000);

$number = '';
$number_len = $strLen;
$stuff = '';//附加碼顯示範圍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;$i<200;$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);

⑵ 求助!!在php中想實現圖片驗證碼的效果

1.rand一個數串。
2.存入cookie中或session中。
3.將數用gd庫方法生成圖片。
4.用戶輸入後與cookie或session對比。
done

⑶ php怎麼做圖片識別

個人建議:樓主使用php進行圖片識別,不如使用ocr文字識別技術來進行圖片識別,這樣更方便,請看下面的方法:

  1. 首先,在電腦上安裝ocr文字識別軟體(迅捷ocr文字識別軟體)。

  2. 接著,運行ocr,選擇上面的『極速識別』功能。

  3. 然後,點擊左上角的『添加文件』,不需要識別的圖片添加進去。

  4. 最後,點擊操作下面的『開始識別』按鈕。

希望上方的方法可以幫助到你。

⑷ 如何用PHP生成驗證碼

PHP生成驗證碼的原理:使用PHP的GD庫,生成一張帶驗證碼的圖片,並將驗證碼保存在Session中。PHP生成驗證碼的大致流程有:

1、產生一張png的圖片;

2、為圖片設置背景色;

3、設置字體顏色和樣式;

4、產生4位數的隨機的驗證碼;

5、把產生的每個字元調整旋轉角度和位置畫到png圖片上;

6、加入噪點和干擾線防止注冊機器分析原圖片來惡意破解驗證碼;

7、輸出圖片;

8、釋放圖片所佔內存。

session_start();
getCode(4,60,20);

functiongetCode($num,$w,$h){
$code="";
for($i=0;$i<$num;$i++){
$code.=rand(0,9);
}
//4位驗證碼也可以用rand(1000,9999)直接生成
//將生成的驗證碼寫入session,備驗證時用
$_SESSION["helloweba_num"]=$code;
//創建圖片,定義顏色值
header("Content-type:image/PNG");
$im=imagecreate($w,$h);
$black=imagecolorallocate($im,0,0,0);
$gray=imagecolorallocate($im,200,200,200);
$bgcolor=imagecolorallocate($im,255,255,255);
//填充背景
imagefill($im,0,0,$gray);

//畫邊框
imagerectangle($im,0,0,$w-1,$h-1,$black);

//隨機繪制兩條虛線,起干擾作用
$style=array($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im,$style);
$y1=rand(0,$h);
$y2=rand(0,$h);
$y3=rand(0,$h);
$y4=rand(0,$h);
imageline($im,0,$y1,$w,$y3,IMG_COLOR_STYLED);
imageline($im,0,$y2,$w,$y4,IMG_COLOR_STYLED);

//在畫布上隨機生成大量黑點,起干擾作用;
for($i=0;$i<80;$i++){
imagesetpixel($im,rand(0,$w),rand(0,$h),$black);
}
//將數字隨機顯示在畫布上,字元的水平間距和位置都按一定波動范圍隨機生成
$strx=rand(3,8);
for($i=0;$i<$num;$i++){
$strpos=rand(1,6);
imagestring($im,5,$strx,$strpos,substr($code,$i,1),$black);
$strx+=rand(8,12);
}
imagepng($im);//輸出圖片
imagedestroy($im);//釋放圖片所佔內存
}

⑸ PHP 繪制網站登錄首頁圖片驗證碼

幾乎所有的網站登錄頁都會有驗證碼,驗證碼是一種安全保護機制,在注冊時要求必須有人工操作進行驗證,用於防止垃圾注冊機大量注冊用戶賬號佔用伺服器內存從而使伺服器癱瘓。
圖片驗證碼的實現十分簡單。首先從指定字元集合中隨機抽取固定數目的字元,以一種不規則的方法畫在畫布上,再適當添加一些干擾點和干擾元素,最後將圖片輸出,一張嶄新的驗證碼就完成了。
先給大家展示下生成的驗證碼:

點擊刷新:

如果大家對實現效果非常滿意,請繼續往下看。
前端代碼如下:
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="content-type"
content="text/html;charset=utf-8">
<title>This
is
a
test!</title>
<link
rel="stylesheet"
type="text/css"
href="css/bootstrap.min.css">
</head>
<body>
<form
name="form">
<input
type="text"
placeholder="賬號"/><br/>
<input
type="password"
placeholder="密碼"/><br/>
<input
type="text"
placeholder="驗證碼"/>
<img
id="verImg"
src="libs/verification.php"/>
<a
href="#"
class="change"
onclick="changeVer()">點擊刷新</a><br/>
<input
type="submit"
value="登錄"/>
</form>
<script
type="text/javascript">
//刷新驗證碼
function
changeVer(){
document.getElementById("verImg").src="libs/verification.php?tmp="+Math.random();
}
</script>
</body>
</html>
php腳本文件驗證碼的代碼如下:
<?php
session_start();
//開啟session記錄驗證碼數據
vCode(4,
15);//設置驗證碼的字元個數和圖片基礎寬度
//vCode
字元數目,字體大小,圖片寬度、高度
function
vCode($num
=
4,
$size
=
20,
$width
=
0,
$height
=
0)
{
!$width
&&
$width
=
$num
*
$size
*
4
/
5
+
15;
!$height
&&
$height
=
$size
+
10;
//設置驗證碼字元集合
$str
=
"";
//保存獲取的驗證碼
$code
=
''
//隨機選取字元
for
($i
=
0;
$i
<
$num;
$i++)
{
$code
.=
$str[mt_rand(0,
strlen($str)-1)];
}
//創建驗證碼畫布
$im
=
imagecreatetruecolor($width,
$height);
//背景色
$back_color
=
imagecolorallocate($im,
mt_rand(0,100),mt_rand(0,100),
mt_rand(0,100));
//文本色
$text_color
=
imagecolorallocate($im,
mt_rand(100,
255),
mt_rand(100,
255),
mt_rand(100,
255));
imagefilledrectangle($im,
0,
0,
$width,
$height,
$back_color);
//
畫干擾線
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);
}
//
畫干擾點
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);
}
//隨機旋轉角度數組
$array=array(5,4,3,2,1,0,-1,-2,-3,-4,-5);
//
輸出驗證碼
//
imagefttext(image,
size,
angle,
x,
y,
color,
fontfile,
text)
@imagefttext($im,
$size
,
array_rand($array),
12,
$size
+
6,
$text_color,
'c:WINDOWSFontssimsun.ttc',
$code);
$_SESSION["VerifyCode"]=$code;
//no-cache在每次請求時都會訪問伺服器
//max-age在請求1s後再次請求會再次訪問伺服器,must-revalidate則第一發送請求會訪問伺服器,之後不會再訪問伺服器
//
header("Cache-Control:
max-age=1,
s-maxage=1,
no-cache,
must-revalidate");
header("Cache-Control:
no-cache");
header("Content-type:
image/png;charset=gb2312");
//將圖片轉化為png格式
imagepng($im);
imagedestroy($im);
}
?>
好了,關於小編給大家介紹的php繪制圖片驗證就給大家介紹這么多,希望對大家有所幫助!

⑹ php怎麼實現驗證碼的

驗證碼功能機制實現思路

  1. 常規的驗證碼實現:

    a、產生一張png的圖片


    b、為圖片設置背景色


    c、設置字體顏色和樣式


    d、產生4位數的隨機的驗證碼


    e、把產生的每個字元調整旋轉角度和位置畫到png圖片上


    f、加入噪點和干擾線防止注冊機器分析原圖片來惡意注冊


    g、輸出圖片


    h、釋放圖片所佔內存


    i、將驗證碼保存到session或是資料庫


    j、將和輸入的驗證碼進行對比

  2. 簡訊(郵箱)驗證碼機制:

    a、產生4-6位數的隨機的驗證碼


    b、把產生的每個字元保存到session或是資料庫


    c、將驗證碼發送到用戶的手機(郵箱)


    d、用戶在規定時間內進行輸入


    e、將驗證碼從session或是資料庫中取出


    f、將和輸入的驗證碼進行對比驗證

⑺ php驗證碼怎麼實現

1.新建code.php驗證碼生成文件

在此之前必須打開php的GD庫,修改php.ini文件的配置,取消extension=php_gd2.dll前面的分號。代碼如下:

<?php

session_start();

//生成驗證碼圖片

Header("Content-type: image/PNG");

$im = imagecreate(44,18);

$back = ImageColorAllocate($im, 245,245,245);

imagefill($im,0,0,$back); //背景

srand((double)microtime()*1000000);

//生成4位數字

for($i=0;$i<4;$i++){

$font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255));

$authnum=rand(1,9);

$vcodes.=$authnum;

imagestring($im, 5, 2+$i*10, 1, $authnum, $font);

}

for($i=0;$i<100;$i++) //加入干擾象素

{

$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));

imagesetpixel($im, rand()p , rand()0 , $randcolor);

}

ImagePNG($im);

ImageDestroy($im);

$_SESSION['Checknum'] = $vcodes;

?>

2. 顯示驗證碼圖片

在需要顯示驗證碼的頁面中加入

<input type="text" name="passcode" >

<img src="code.php">

3.判斷並獲取驗證碼的值

驗證碼是通過第一步驟代碼中的$_SESSION['Checknum'] = $vcodes;賦的值,所以驗證碼的值存在$_SESSION['Checknum']當中。在驗證頁面,使用以下代碼,

...

session_start();//啟動會話

$code=$_POST["passcode"];

if( $code == $_SESSION["Checknum"])

{...}即可完成驗證碼登錄。

運行截圖:

望採納,謝謝

⑻ 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;$i<80;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//畫隨機出現的線
for ($x=0; $x<9;$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;
}
?>

⑼ 如何用PHP寫圖片驗證碼驗證代碼

你去php100看看視頻教程吧,踏踏實實的看完了,你的問題也就解決了

⑽ 像這樣的驗證碼有沒有辦法用 PHP 識別

一般沒辦法 只能用一些辦法來搞 比如curl 把圖片存下來 然後用txt寸你看到的驗證碼 然後PHP讀取txt內容 或者你用一些打碼平台!

閱讀全文

與php圖片驗證碼識別相關的資料

熱點內容
網盤忘記解壓碼怎麼辦 瀏覽:852
文件加密看不到裡面的內容 瀏覽:651
程序員腦子里都想什麼 瀏覽:430
oppp手機信任app在哪裡設置 瀏覽:185
java地址重定向 瀏覽:268
一年級下冊摘蘋果的演算法是怎樣的 瀏覽:448
程序員出軌電視劇 瀏覽:88
伺服器系統地址怎麼查 瀏覽:54
解壓游戲發行官 瀏覽:601
國外小伙解壓實驗 瀏覽:336
頂級大學開設加密貨幣 瀏覽:437
java重載與多態 瀏覽:528
騰訊應屆程序員 瀏覽:942
一鍵編譯程序 瀏覽:129
語音加密包哪個好 瀏覽:340
有什麼學習高中語文的app 瀏覽:282
安卓手機的表格里怎麼打勾 瀏覽:411
阿里雲伺服器有網路安全服務嗎 瀏覽:970
超解壓兔子視頻 瀏覽:24
單片機怎麼測負脈沖 瀏覽:174