php 简单图片验证码,PHP 实现简单图片验证码
驗證碼是網站會員系統中不可缺少的,目前驗證碼有很多種,但用的比較多的還是圖片驗證碼,這里就用面向對象的方式來簡單實現圖片驗證碼,
注意!我這里使用的是 PHP 的 gd 庫,如果要查看是否啟用了 gd 庫可以建立一個 phpinfo.php 文件,在文件中加入一句 phpinfo() ,訪問 phpinfo.php 就可以查看是否啟用了 gd 庫,如果 GD Support 為 enabled 就是已啟用。
如果要在 Windows 啟用 gd 庫可以打開 PHP 的安裝目錄,用文本編輯器打開 php.ini ,去除 xtension=php_gd2.dll 前面的注釋即可。
PHP類
PHP代碼:<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/10/6
* Time: 20:44
*/
class Verification {
protected $fontfile = ''; // 字體文件
protected $width = 120; // 圖片寬度 默認120
protected $height = 40; // 圖片高度 默認40
protected $size = 20; // 字體大小 默認20
protected $length = 4; // 長度 默認4個字
protected $image = null; // 畫布資源
protected $snow = 0; // 雪花干擾 默認沒有干擾
protected $pixel = 0; // 像素點干擾 默認沒有干擾
protected $line = 0; // 線段干擾 默認沒有干擾
// 初始化數據
public function __construct($config = array()) {
if (is_array($config) && count($config) > 0) {
// 檢測字體文件是否存在
if (isset($config['fontfile']) && is_file($config['fontfile'])) {
$this->fontfile = $config['fontfile']; // 設置字體文件位置
}else {
return false;
}
// 判斷是否傳入了寬度和寬度是否大于0
if (isset($config['width']) && $config['width'] > 0) {
// 設置寬度屬性為傳入的寬度
$this->width = (int)$config['width'];
}
// 判斷是否傳入高度和高度是否大于0
if (isset($config['height']) && $config['height'] > 0) {
$this->height = (int)$config['height']; // 設置高度屬性
}
// 判斷是否傳入了字體大小和是否大于0
if (isset($config['size']) && $config['size'] > 0) {
$this->size = (int)$config['size']; // 設置字體大小屬性
}
// 判斷是否傳入了驗證碼長度和長度是否大于0
if (isset($config['length']) && $config['length'] > 0) {
$this->length = (int)$config['length']; // 設置長度
}
// 判斷是否設置了雪花干擾以及雪花數量是否大于0
if (isset($config['snow']) && $config['lsnow'] > 0) {
$this->snow = (int)$config['snow']; // 設置雪花干擾
}
// 判斷是否設置了像素點干擾和數量是否大于0
if (isset($config['pixel']) && $config['pixel'] > 0) {
$this->pixel = (int)$config['pixel']; // 設置像素點干擾
}
// 判斷是否設置了線條干擾和線條數量
if (isset($config['line']) && $config['line'] > 0) {
$this->line = (int)$config['line'];
}
$this->image = imagecreatetruecolor($this->width,$this->height); // 創建畫布
return $this->image; // 返回i畫布資源
}else {
return false;
}
}
// 生成驗證碼
public function getCaptcha() {
// 創建白色作為背景
$white = imagecolorallocate($this->image,255, 255, 255);
// 填充背景顏色
imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $white);
// 調用生成驗證碼內容的方法
$str = $this->generateStr($this->length);
// 判斷生成內容是否成功
if (false === $str) {
return false;
}
$fontfile = $this->fontfile; // 設置字體文件位置
// 使用for循環來生成圖片驗證碼,循環次數是驗證碼的長度
for ($i = 0;$i < $this->length;$i ++) {
$size = $this->size; // 設置字體大小
$angle = rand(-30, 30); // 設置字體角度
$x = ceil($this->width / $this->length) * $i + rand(5, 10); // 設置X軸位置
$y = ceil($this->height / 1.5); // 設置X軸位置
// 設置字體顏色,調用生成隨機顏色的方法
$color = $this->getRandColor();
$text = $str{$i}; // 設置驗證碼內容
// 把驗證碼添加到畫布上
imagettftext($this->image, $size, $angle, $x, $y, $color, $fontfile, $text);
}
// 是否需要雪花干擾
if ($this->snow > 0) {
$this->getSnow();
}else {
// 只有在沒有雪花干擾的情況下才可以使用像素點和線段干擾
if ($this->pixel) {
$this->getPixel();
}
if ($this->line) {
$this->getLine();
}
}
header('content-type:image/png'); // 設置顯示方式
imagepng($this->image); // 把生成的驗證碼圖片顯示在網頁上
imagedestroy($this->image); // 銷毀畫布 節省資源
return strtolower($str); // 返回生成的驗證碼內容,用來設置session驗證
}
// 生成雪花干擾
protected function getSnow() {
for ($i = 0;$i < $this->snow;$i ++) {
imagestring($this->image, rand(1, 5), rand(0, $this->width), rand(0, $this->height), '*', $this->getRandColor());
}
}
// 生成像素點干擾
protected function getPixel() {
for ($i = 0;$i < $this->pixel;$i ++) {
imagesetpixel($this->image, rand(0, $this->width), rand(0, $this->height), $this->getRandColor());
}
}
// 線段干擾
protected function getLine() {
for ($i = 0;$i < $this->line;$i ++) {
imageline($this->image, rand(0, $this->width), rand(0, $this->height), rand(0, $this->width), rand(0, $this->height), $this->getRandColor());
}
}
// 生成驗證碼字符
protected function generateStr($length = 4) {
// 判斷驗證碼長度是否小于1或大于30
if ($length < 1 || $length > 30) {
return false;
}else {
// 驗證碼可能出現的內容
$chars = array(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S','T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'1', '2', '3', '4', '5', '6', '7', '8', '9'
);
}
// 取出指定長度的驗證碼
$str = join('', array_rand(array_flip($chars), $length));
return $str;
}
// 用來生成隨機顏色的
protected function getRandColor() {
return imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
}
}
使用方法
對象接收一個數組,除字體文件以外其他參數可以省略,省略的參數會使用默認參數,下面是簡單說明:參數類型參數可省略說明fontfilestring否字體文件名稱
widthint是圖片寬度,默認120px
heightint是圖片高度,默認40px
sizeint是字體大小,默認20px
lengthint是驗證碼長度,默認4個字
snowint是雪花干擾,默認為0,沒有干擾
pixelint是像素點干擾,默認為0,沒有干擾
lineint是線條干擾,默認為0,沒有干擾
代碼:<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/10/8
* Time: 16:55
*/
require_once 'verification_class.php'; // 引入類文件
$arr = array(
'fontfile'=>'fonts/arial.ttf', // 字體文件位置
'line'=>4, // 設置線條干擾數量為4
'pixel'=>50 // 設置像素點干擾為50
);
$img = new Verification($arr); // 實例化對象并且傳入數組
session_start(); // 啟用session
$_SESSION['str'] = $img->getCaptcha(); // 把返回的驗證碼保存到session
完整地傳入數組參數可在類屬性看到
最終效果如下:
驗證碼的每一個字都是隨機顏色,角度也是隨機的,如果要讓驗證碼在 img 標簽顯示 就把 img 的 src 設置為調用類的 php 文件即可。
驗證方法
直接判斷表單提交的參數和 session 保存的驗證碼是否一致即可。
這就是簡單實現圖片驗證碼的方法,這個驗證碼還有很大的優化空間。
代碼下載可點擊下方的下載代碼鏈接,百度網盤提取碼:w39g
總結
以上是生活随笔為你收集整理的php 简单图片验证码,PHP 实现简单图片验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: s3cmd安装配置及基础命令
- 下一篇: PHP开发环境搭建