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