先谈谈代码的顺序
1.准备画布
2.分配颜色
3.使用相关函数进行绘画
4.告诉别人你画的是什么
5.输出或者保存图像
6.释放资源
先写一个简单的
<?php
//1.准备画布
$img = imagecreatetruecolor(500,500);
//2.分配颜色
$back = imagecolorallocate($img,255,0,0);
$fontcolor = imagecolorallocate($img,0,0,255);
$color = imagecolorallocate($img,0,0,0);
//echo $name;
//3.绘画
//为背景填充颜色
imagefill($img,0,0,$back);
//写字
imagechar($img,10,10,10,'H',$fontcolor);
//画点
for($i=0;$i<10000;$i++){
imagesetpixel($img,mt_rand(1,499),mt_rand(1,499),$color);
}
//画线
for($i=0;$i<20;$i++){
//随机颜色
$linecolor = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($img,mt_rand(1,499),mt_rand(1,499),mt_rand(1,499),mt_rand(1,499),$linecolor);
}
//写字
$color = imagecolorallocate($img,255,255,0);
imagettftext($img,mt_rand(10,20),0,70,70,$color,'./msyh.ttf','哈哈哈,随便写点');
//4.告诉浏览器你画的是什么
header('Content-type:image/jpeg');
//5.输出到浏览器 对应图片的格式输出
imagejpeg($img);
//6.释放资源
imagedestroy($img);
那我们写一个类,来方便我们使用
<?php
//写一个验证码类
class Vcode{
//成员属性
//画布宽
private $width;
//画布高
private $height;
//画布资源
private $img;
//写字的数量
private $codeNum;
//字体文件
private $fontFile;
//成员方法
//初始化画布的宽 高
function __construct($fontfile = '',$width = 100,$height = 40,$codeNum = 4){
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->fontFile = $fontfile;
}
//定义一个输出的方法
public function show(){
//调用点
//创建画布
$this->createImage();
//设置干扰点
$this->setpixel();
//设置干扰先
$this->imgLine();
//设置验证码字体
$this->createFont();
//显示验证码图片
$this->showImage();
}
//定义一个魔术方法__tostring()
function __toString(){
$this->show();
return '';
}
//准备创建画布的方法
private function createImage(){
//创建真彩色画布
$this->img = imagecreatetruecolor($this->width,$this->height);
//为画布分配背景颜色
$back = imagecolorallocate($this->img,mt_rand(220,255),mt_rand(220,255),mt_rand(200,255));
//填充背景颜色
imagefill($this->img,0,0,$back);
//给背景设置边框
//分配边框颜色
$borderColor = imagecolorallocate($this->img,255,0,0);
//画一个矩形
imagerectangle($this->img,0,0,$this->width-1,$this->height-1,$borderColor);
}
//准备显示图像的方法
private function showImage(){
header("Content-type:image/jpeg");
imagejpeg($this->img);
}
//准备画干扰线的方法
private function imgLine(){
//循环10条线
for($i=0;$i<10;$i++){
//为线分配随机颜色
$lineColor = imagecolorallocate($this->img,mt_rand(150,180),mt_rand(150,180),mt_rand(150,180));
imageline($this->img,mt_rand(2,$this->width-2),mt_rand(2,$this->height-2),mt_rand(2,$this->width-2),mt_rand(2,$this->height-2),$lineColor);
}
}
//准备画干扰点的方法
private function setpixel(){
//循环画干扰点
for($i=0;$i<300;$i++){
//为干扰点随机分配颜色
$pixelColor = imagecolorallocate($this->img,mt_rand(120,150),mt_rand(120,150),mt_rand(120,150));
//画点
imagesetpixel($this->img,mt_rand(2,$this->width-2),mt_rand(2,$this->height-2),$pixelColor);
}
}
//获取字符方法
private function getFont(){
$ascii = '';
$str = '345678ABCDEFGHJKLMNPQRSTUVWXYabcdefhijkmnpqrstuvwxy';
//随机四个数
for($i=0;$i<$this->codeNum;$i++){
//每次循环都随机打乱一次字符串
$str = str_shuffle($str);
//获取四个字
$char = $str{mt_rand(0,strlen($str)-1)};
//str_shuffle();//随机打乱字符
$ascii .= $char;
}
//存验证码中的数字
$_SESSION['code'] = $ascii;
//返回拼接好后的四个字符
return $ascii;
}
//写字的方法
private function createFont(){
//获取要写入的字符
$ascii = $this->getFont();
//判断是否传入 字体文件 如果传入 则使用字体文件的字体来书写验证码
if($this->fontFile != ''){
//传入了字体库文件 使用freetype字体写入
for($i=0;$i<$this->codeNum;$i++){
//随机分配字体颜色
$fontColor = imagecolorallocate($this->img,mt_rand(10,90),mt_rand(10,90),mt_rand(10,90));
//获取x 的位置
$x = $i*($this->width/$this->codeNum)+mt_rand(5,8);
//获取Y的位置
$y = mt_rand($this->height/2,$this->height-3);
//使用freetype字体写入文件
imagettftext($this->img,mt_rand(15,20),mt_rand(0,40),$x,$y,$fontColor,$this->fontFile,$ascii{$i});
}
}else{
//没有传入字体文件
for($i=0;$i<$this->codeNum;$i++){
//随机分配字体颜色
$fontColor = imagecolorallocate($this->img,mt_rand(10,90),mt_rand(10,90),mt_rand(10,90));
$x = $i*($this->width/$this->codeNum)+mt_rand(3,8);
$y = mt_rand(10,20);
imagechar($this->img,mt_rand(3,5),$x,$y,$ascii{$i},$fontColor);
}
}
}
//析构方法 销毁资源
function __destruct(){
//销毁图像资源
imagedestroy($this->img);
}
}
// $c = new Vcode('./msyh.ttf');
// //$c->show();
// echo $c;
本文介绍如何使用PHP生成包含随机字符的验证码图像,并通过类的方式组织代码。涵盖了画布创建、颜色分配、绘制背景、添加随机线条和点等步骤。
855

被折叠的 条评论
为什么被折叠?



