驗證碼實現

2021-07-25 15:25:26 字數 2471 閱讀 4807

1. gd庫

2.建立影象步驟

(1)建立畫布:就是在記憶體中開闢一塊臨時區域,用於儲存圖影象資訊;

(2)繪製影象:使用各種函式設定影象顏色,背景,填充畫筆,繪製圖形等;

(3)輸出圖形:以某種格式儲存到伺服器或者輸出到瀏覽器顯示給使用者。

注意:直接輸出之前一定要用header( )告訴瀏覽器以影象格式來處理該輸出,比如header(『content-type: image/png』);

(4)釋放資源:節約系統資源考慮,需及時清除。

1. 原理

在乙個矩形畫布上面生成隨機字母數字或漢字,每個字型設定隨機的顏色和旋轉角度(小於90度),畫布加上背景色和一些干擾元素(點、線、弧)

2. **

<?php

/** * 驗證碼類

*@author prkom

*/class

code

$this->font = basepath . "fonts/font.ttf";

if (!is_file($this->font))

$this->width = empty($width) ? $this->width : $width;

$this->height = empty($height) ? $this->height : $height;

$this->bgcolor = empty($bgcolor) ? $this->bgcolor : $bgcolor;

$this->codelen = empty($codelen) ? $this->codelen : $codelen;

$this->fontsize = empty($fontsize) ? $this->fontsize : $fontsize;

$this->fontcolor = empty($fontcolor) ? $this->fontcolor : $fontcolor;

$this->create();//生成驗證碼

}/**

* 返回驗證碼

*/public

function

getcode

() /**

* 建畫布

*/public

function

create

() /**

* 畫線

*/private

function

createline

() $line_width = $w/10;

for($i=0;$i

<10;$i++)

}/**

* 寫入驗證碼文字

*/private

function

createfont

() $x = ($this->width - 10) / $this->codelen;

for ($i = 0; $i

< $this->codelen; $i++)

imagettftext($this->img, $this->fontsize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontcolor, $this->font, $this->code [$i]);

}$this->fontcolor = $fontcolor;

}/**

* 干擾元素

*/private

function

createpix

() //畫線

for ($i = 0; $i

< 2; $i++)

//畫圓弧

for ($i = 0; $i

< 1; $i++)

imagesetthickness($this->img, 1); //設定畫線的寬度

}/**

* 畫矩形邊框

*/private

function

createrec

() /**

* 驗證gd庫是否開啟imagepng函式是否可用

*/private

function

checkgd

() /**

* 生成隨機驗證碼

*/private

function

createcode

() $this->code = strtoupper($code);

if(!isset($_session))

$_session ['code'] = $this->code;

}/**

* 顯示驗證碼

*/public

function

show

() }

php驗證碼zhuc php實現驗證碼製作

php實現驗證碼製作 首先,看一張圖了解驗證碼生成的過程。1 生成驗證碼底圖 2 驗證碼內容 3 生成驗證碼 4 對比校驗 驗證碼實現的核心技術分析 a 底圖的 實現,並新增干擾元素 b 生成驗證內容 c 驗證內容儲存在服務端 d 驗證內容的校驗 下面看 實現的過程 這段 實現了產生 隨機數字,隨機...

PHP實現驗證碼

目前,不少 為了防止使用者利用機械人自動註冊 登入 灌水,都採用了驗證碼技術。所謂驗證碼,就是將一串隨機產生的數字或符號,生成一幅,裡加上一些干擾象素 防止 ocr 由使用者肉眼識別其中的驗證碼資訊,輸入表單提交 驗證,驗證成功後才能使用某項功能。我們這裡展示了如何編寫 php程式實現驗證碼功能 一...

jfinal Patchca 驗證碼實現

public class registcontroller extends controller author 版本 1 修改日期 2014 06 27 說明 public class patchcarender extends render catch ioexception e finally ...