本文目錄一覽:
- 1、php用GD庫怎麼隨機產生彩色字母加數字的驗證碼
- 2、怎麼用php生成圖像,生成驗證碼
- 3、php怎麼實現驗證碼的
- 4、PHP滑動拼圖驗證碼的圖片是怎樣生成的
- 5、求助!!在php中想實現圖片驗證碼的效果
php用GD庫怎麼隨機產生彩色字母加數字的驗證碼
這個問題比較簡單,只講下原理,代碼如果你要的話我發給你,或者自己網上搜索下。
原理:
1、創建0-9a-zA-Z字符串
2、創建GD畫布,選擇相應的顏色
3、用for循環產生制定長度的【驗證碼】元素(使用隨機數函數來獲取不同的字符)
4、以驗證碼元素為內容創建圖像
5,如果需要對每個字符設置不同的顏色,只需要對每個字符單獨設置一個前景色即可
怎麼用php生成圖像,生成驗證碼
?php
//驗證碼類
class ValidateCode {
private $charset = ‘abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789’;//隨機因子
private $code;//驗證碼
private $codelen = 4;//驗證碼長度
private $width = 90;//寬度
private $height = 40;//高度
private $img;//圖形資源句柄
private $font;//指定的字體
private $fontsize = 20;//指定字體大小
private $fontcolor;//指定字體顏色
//構造方法初始化
public function __construct() {
$this-font = dirname(__FILE__).’/font/elephant.ttf’;//注意字體路徑要寫對,否則顯示不了圖片
}
//生成隨機碼
private function createCode() {
$_len = strlen($this-charset)-1;
for ($i=0;$i$this-codelen;$i++) {
$this-code .= $this-charset[mt_rand(0,$_len)];
}
}
//生成背景
private function createBg() {
$this-img = imagecreatetruecolor($this-width, $this-height);
$color = imagecolorallocate($this-img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this-img,0,$this-height,$this-width,0,$color);
}
//生成文字
private function createFont() {
$_x = $this-width / $this-codelen;
for ($i=0;$i$this-codelen;$i++) {
$this-fontcolor = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this-img,$this-fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this-height / 1.4,$this-fontcolor,$this-font,$this-code[$i]);
}
}
//生成線條、雪花
private function createLine() {
//線條
for ($i=0;$i6;$i++) {
$color = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this-img,mt_rand(0,$this-width),mt_rand(0,$this-height),mt_rand(0,$this-width),mt_rand(0,$this-height),$color);
}
//雪花
for ($i=0;$i100;$i++) {
$color = imagecolorallocate($this-img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this-img,mt_rand(1,5),mt_rand(0,$this-width),mt_rand(0,$this-height),’*’,$color);
}
}
//輸出
private function outPut() {
header(‘Content-type:image/png’);
imagepng($this-img);
imagedestroy($this-img);
}
//對外生成
public function doimg() {
$this-createBg();
$this-createCode();
$this-createLine();
$this-createFont();
$this-outPut();
}
//獲取驗證碼
public function getCode() {
return strtolower($this-code);
}
}
php怎麼實現驗證碼的
驗證碼功能機制實現思路
常規的驗證碼實現:
a、產生一張png的圖片
b、為圖片設置背景色
c、設置字體顏色和樣式
d、產生4位數的隨機的驗證碼
e、把產生的每個字符調整旋轉角度和位置畫到png圖片上
f、加入噪點和干擾線防止註冊機器分析原圖片來惡意註冊
g、輸出圖片
h、釋放圖片所佔內存
i、將驗證碼保存到session或是數據庫
j、將和輸入的驗證碼進行對比
短信(郵箱)驗證碼機制:
a、產生4-6位數的隨機的驗證碼
b、把產生的每個字符保存到session或是數據庫
c、將驗證碼發送到用戶的手機(郵箱)
d、用戶在規定時間內進行輸入
e、將驗證碼從session或是數據庫中取出
f、將和輸入的驗證碼進行對比驗證
PHP滑動拼圖驗證碼的圖片是怎樣生成的
1 如果放在項目中用,驗證碼圖片希望可以是接口返回。ImageView以及其子類支持花式加載圖片。
2 繼承自ImageView,繪製圖片本身不用我們干預,也不用我們操心scaleType,節省很多工作。
* 在onSizeChanged()方法中生成 和 控件寬高相關的屬性值:
1 初始化時隨機生成驗證碼區域起點
2 生成驗證碼區域Path
3 生成滑塊Bitmap
* onDraw()時,依次繪製:
1 驗證碼陰影
2 滑塊
求助!!在php中想實現圖片驗證碼的效果
1.rand一個數串。
2.存入cookie中或session中。
3.將數用gd庫方法生成圖片。
4.用戶輸入後與cookie或session對比。
done
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/246483.html