搜尋
首頁後端開發php教程基於GD庫的php驗證碼類且支援中英文字體、背景、幹擾點線、扭曲等

基於GD庫的php驗證碼類別(支援中英文字型、背景、幹擾點線、扭曲.......) 

<?php
/*
* Captcha Class base on PHP GD Lib
* @author Design
* @version 1.0
* @copyright js8.in 2010
* @demo
* include(&#39;captchaClass.php&#39;);
* $captchaDemo=new Captcha();
* $captchaDemo->createImage();
*/
class Captcha
{
//@定义验证码图片高度
private $height;
//@定义验证码图片宽度
private $width;
//@定义验证码字符个数
private $textNum;
//@定义验证码字符内容
private $textContent;
//@定义字符颜色
private $fontColor;
//@定义随机出的文字颜色
private $randFontColor;
//@定义字体大小
private $fontSize;
//@定义字体
private $fontFamily;
//@定义背景颜色
private $bgColor;
//@定义随机出的背景颜色
private $randBgColor;
//@定义字符语言
private $textLang;
//@定义干扰点数量
private $noisePoint;
//@定义干扰线数量
private $noiseLine;
//@定义是否扭曲
private $distortion;
//@定义扭曲图片源
private $distortionImage;
//@定义是否有边框
private $showBorder;
//@定义验证码图片源
private $image;

//@Constructor 构造函数
public function Captcha()
{
$this->textNum = 4;
$this->fontSize = 16;
$this->fontFamily = &#39;c:\windows\fontsSIMYOU.ttf&#39;; //设置中文字体,可以改成linux的目录
$this->textLang = &#39;en&#39;;
$this->noisePoint = 30;
$this->noiseLine = 3;
$this->distortion = false;
$this->showBorder = false;
}


//@设置图片宽度
public function setWidth($w)
{
$this->width = $w;
}

//@设置图片高度
public function setHeight($h)
{
$this->height = $h;
}

//@设置字符个数
public function setTextNumber($textN)
{
$this->textNum = $textN;
}

//@设置字符颜色
public function setFontColor($fc)
{
$this->fontColor = sscanf($fc, &#39;#%2x%2x%2x&#39;);
}

//@设置字号
public function setFontSize($n)
{
$this->fontSize = $n;
}

//@设置字体
public function setFontFamily($ffUrl)
{
$this->fontFamily = $ffUrl;
}

//@设置字符语言
public function setTextLang($lang)
{
$this->textLang = $lang;
}

//@设置图片背景
public function setBgColor($bc)
{
$this->bgColor = sscanf($bc, &#39;#%2x%2x%2x&#39;);
}

//@设置干扰点数量
public function setNoisePoint($n)
{
$this->noisePoint = $n;
}

//@设置干扰线数量
public function setNoiseLine($n)
{
$this->noiseLine = $n;
}

//@设置是否扭曲
public function setDistortion($b)
{
$this->distortion = $b;
}

//@设置是否显示边框
public function setShowBorder($border)
{
$this->showBorder = $border;
}

//@初始化验证码图片
public function initImage()
{
if (empty($this->width)) {
$this->width = floor($this->fontSize * 1.3) * $this->textNum + 10;
}
if (empty($this->height)) {
$this->height = $this->fontSize * 2;
}
$this->image = imagecreatetruecolor($this->width, $this->height);
if (empty($this->bgColor)) {
$this->randBgColor = imagecolorallocate($this->image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255));
} else {
$this->randBgColor = imagecolorallocate($this->image, $this->bgColor[0], $this->bgColor[1], $this->bgColor[2]);
}
imagefill($this->image, 0, 0, $this->randBgColor);
}

//@产生随机字符
public function randText($type)
{
$string = &#39;&#39;;
switch ($type) {
case &#39;en&#39;:
$str = &#39;ABCDEFGHJKLMNPQRSTUVWXY3456789&#39;;
for ($i = 0; $i < $this->textNum; $i++) {
$string = $string . &#39;,&#39; . $str[mt_rand(0, 29)];
}
break;
case &#39;cn&#39;:
for ($i = 0; $i < $this->textNum; $i++) {
$string = $string . &#39;,&#39; . chr(rand(0xB0, 0xCC)) . chr(rand(0xA1, 0xBB));
}
$string = iconv(&#39;GB2312&#39;, &#39;UTF-8&#39;, $string); //转换编码到utf8
break;
}
return substr($string, 1);
}

//@输出文字到验证码
public function createText()
{
$textArray = explode(&#39;,&#39;, $this->randText($this->textLang));
$this->textContent = join(&#39;&#39;, $textArray);
if (empty($this->fontColor)) {
$this->randFontColor = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
} else {
$this->randFontColor = imagecolorallocate($this->image, $this->fontColor[0], $this->fontColor[1], $this->fontColor[2]);
}
for ($i = 0; $i < $this->textNum; $i++) {
$angle = mt_rand(-1, 1) * mt_rand(1, 20);
imagettftext($this->image, $this->fontSize, $angle, 5 + $i * floor($this->fontSize * 1.3), floor($this->height * 0.75), $this->randFontColor, $this->fontFamily, $textArray[$i]);
}
}

//@生成干扰点
public function createNoisePoint()
{
for ($i = 0; $i < $this->noisePoint; $i++) {
$pointColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $pointColor);
}

}

//@产生干扰线
public function createNoiseLine()
{
for ($i = 0; $i < $this->noiseLine; $i++) {
$lineColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), 20);
imageline($this->image, 0, mt_rand(0, $this->width), $this->width, mt_rand(0, $this->height), $lineColor);
}
}

//@扭曲文字
public function distortionText()
{
$this->distortionImage = imagecreatetruecolor($this->width, $this->height);
imagefill($this->distortionImage, 0, 0, $this->randBgColor);
for ($x = 0; $x < $this->width; $x++) {
for ($y = 0; $y < $this->height; $y++) {
$rgbColor = imagecolorat($this->image, $x, $y);
imagesetpixel($this->distortionImage, (int) ($x + sin($y / $this->height * 2 * M_PI - M_PI * 0.5) * 3), $y, $rgbColor);
}
}
$this->image = $this->distortionImage;
}

//@生成验证码图片
public function createImage()
{
$this->initImage(); //创建基本图片
$this->createText(); //输出验证码字符
if ($this->distortion) {
$this->distortionText();
} //扭曲文字
$this->createNoisePoint(); //产生干扰点
$this->createNoiseLine(); //产生干扰线
if ($this->showBorder) {
imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->randFontColor);
} //添加边框
imagepng($this->image);
imagedestroy($this->image);
if ($this->distortion) {
imagedestroy($this->$distortionImage);
}
return $this->textContent;
}

}
?>

使用方法:

<?php
//session_start();
header("Content-type:image/png");
include(&#39;captcha5_class.php&#39;);
$captcha5 = new Captcha();

//@设置验证码宽度
//$captcha5->setWidth(200);

//@设置验证码高度
//$captcha5->setHeight(50);

//@设置字符个数
$captcha5->setTextNumber(5);

//@设置字符颜色
//$captcha5->setFontColor(&#39;#ff9900&#39;);

//@设置字号大小
//$captcha5->setFontSize(25);

//@设置字体
$captcha5->setFontFamily(&#39;c:\windows\fonts\STXINGKA.TTF&#39;);

//@设置语言
$captcha5->setTextLang(&#39;cn&#39;);

//@设置背景颜色
//$captcha5->setBgColor(&#39;#000000&#39;);

//@设置干扰点数量
//$captcha5->setNoisePoint(600);

//@设置干扰线数量
//$captcha5->setNoiseLine(10);

//@设置是否扭曲
//$captcha5->setDistortion(true);

//@设置是否显示边框
$captcha5->setShowBorder(true);

//输出验证码
$code = $captcha5->createImage();
//$_SESSION[&#39;captchaCode&#39;][&#39;content&#39;]=$code;
//$_SESSION[&#39;captchaCode&#39;][&#39;time&#39;]=microtime();
?>
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP中的GD库操作指南PHP中的GD库操作指南May 20, 2023 pm 02:40 PM

一、什么是GD库?GD库是一组用于创建和处理各种图像格式的库函数,是PHP中最为常用的图像处理库之一。二、安装GD库在CentOS/RedHat下安装GD库1.安装PHP的GD扩展库yuminstallphp-gd2.重启web服务器servicehttpdrestart3.查看PHP支持的GD库版本php-i|grep-igd在Ubunt

php无法开启gd 库怎么办php无法开启gd 库怎么办Nov 18, 2022 am 10:31 AM

php无法开启gd库的解决办法:1、找到并打开php.ini配置文件;2、将“extension_dir”前面的注释符号“;”去掉;3、将其值改为ext文件夹的绝对路径即可。

利用PHP和GD库实现圆角图片的方法利用PHP和GD库实现圆角图片的方法Jul 12, 2023 am 09:21 AM

利用PHP和GD库实现圆角图片的方法介绍在网页设计中,有时需要使用圆角图片来美化页面的外观。本文将介绍如何使用PHP和GD库来实现圆角图片的方法。GD库是PHP扩展库之一,提供了一系列处理图像的函数。通过使用GD库,我们可以对图片进行裁剪、调整尺寸、添加滤镜等操作。而要实现圆角图片,我们需要利用GD库中的一些函数进行图像的处理。步骤以下是实现圆角图片的具体步

利用PHP和GD库实现图片旋转的方法利用PHP和GD库实现图片旋转的方法Jul 12, 2023 am 11:52 AM

利用PHP和GD库实现图片旋转的方法图片旋转是一个常见的图像处理需求,通过旋转图片可以实现一些特殊的效果或满足用户需求。在PHP中,可以借助GD库来实现图片旋转功能。本文将介绍如何使用PHP和GD库来实现图片旋转,并附带代码示例。首先,确保你的PHP环境已经安装了GD库拓展。在命令行中输入php-m,查看是否有gd模块,如果没有则需要先安装。下面是一个简单

使用PHP和GD库创建图像缩略图的步骤使用PHP和GD库创建图像缩略图的步骤Jul 12, 2023 am 08:03 AM

标题:使用PHP和GD库创建图像缩略图的步骤引言:在Web开发中,图像常常需要进行缩略处理以适应不同的页面布局。本文将介绍如何使用PHP和GD库来创建图像缩略图的步骤,并附上相关代码示例。一、安装和配置GD库GD库是一个用于图像处理的库,可以使用一些简单的函数来处理图像。在开始之前,我们需要确保GD库已正确安装和配置。检查GD库是否已经安装:在PHP脚本中执

PHP和GD库实现图片裁剪的方法PHP和GD库实现图片裁剪的方法Jul 14, 2023 am 08:57 AM

PHP和GD库实现图片裁剪的方法概述:图片裁剪是网页开发中常见的需求之一,它可以用于调整图片的尺寸,剪裁不需要的部分,以适应不同的页面布局和展示需求。在PHP开发中,我们可以借助GD库来实现图片裁剪的功能。GD库是一个强大的图形库,可提供一系列函数来处理和操控图像。代码示例:下面我们将详细介绍如何使用PHP和GD库来实现图片裁剪。首先,确保你的PHP环境已经

PHP和GD库指南:如何根据像素绘制图形PHP和GD库指南:如何根据像素绘制图形Jul 12, 2023 pm 11:45 PM

PHP和GD库指南:如何根据像素绘制图形引言:在Web开发中,经常需要使用图形来增强用户界面或显示特定的数据。PHP是一种流行的服务器端编程语言,它提供了GD库来处理图像。本文将详细介绍如何使用PHP和GD库根据像素绘制各种图形,并附带代码示例。内容:一、准备工作:在开始之前,请确保你已经安装了PHP和GD库。可以通过以下命令来检查是否安装:php-m|

利用PHP和GD库生成随机背景图片利用PHP和GD库生成随机背景图片Jul 13, 2023 pm 12:30 PM

利用PHP和GD库生成随机背景图片随机背景图片在网页设计中起着重要的作用,可以增加页面的美观性和吸引力。本文将介绍如何使用PHP和GD库来生成随机背景图片。GD库是一个用于图像处理的PHP扩展模块,可以在PHP中创建、编辑和操作图像。通过结合GD库的强大功能,我们可以轻松地生成各种风格的随机背景图片。首先,我们需要在服务器上安装GD库。你可以通过以下命令来检

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中