在网络上尤为是须要登陆的网站,验证码简直是到处可见,验证码的形式也是五花八门:静态图片,动态图片,语音,滑块.
而如今我就打算作一个本身的生成图片形式的验证码类,固然,用的是我吃饭的语言php.
gd库是必须的,对应php.ini里的gd2扩展必须打开
先建立个类,好比我起了个名字gcudCaptcha,而后声明几个基础属性php
<?php namespace gcud; class gcudCaptcha { //单位彷佛是像素 private $Weight=150,$Height=50; }
之前我是用过gd库,借此机会也正好熟悉下
首先须要建立一个指定宽高的画布,因而写个方法浏览器
private function CreateImage(){ //gd库用于建立画布的方法 imagecreate($this->Weight,$this->Height); }
同时再加个$Image属性,用于接收建立的图像网络
private $Image; private function CreateImage(){ $this->Image=imagecreate($this->Weight,$this->Height); }
因为建立的图像须要直接输出在浏览器上,再写个输出的方法函数
private function Output(){ //让浏览器识别图片 header('Content-Type:image/jpeg'); //设定输出类型为jpeg图像 imagejpeg($this->Image); //记得释放资源 imagedestroy($this->Image); }
另外须要对外暴露一个生成的方法字体
public function Generate(){ $this->Output(); }
并且在初始化时要调用建立图像网站
public function __construct() { $this->CreateImage(); }
因而如今整个代码变成了ui
<?php namespace gcud; class gcudCaptcha { private $Weight = 150, $Height = 50, $Image; public function __construct() { $this->CreateImage(); } private function CreateImage() { $this->Image = imagecreate($this->Weight, $this->Height); } private function Output() { header('Content-Type:image/jpeg'); imagejpeg($this->Image); imagedestroy($this->Image); } public function Generate() { $this->Output(); } }
随便在一个php中调用this
<?php //个人类是放到这里的 require_once 'Vendor\gcud\gcudCaptcha.php'; $gcudCaptcha=new \gcud\gcudCaptcha(); $gcudCaptcha->Generate();
能够看到浏览器输出了一个黑色的图片,接下来设定一个背景色,设定背景色RGB属性,并写在建立图像函数里面spa
private $BackgroundColorRGB=[127,127,127]; private function CreateImage() { $this->Image = imagecreate($this->Weight, $this->Height); imagecolorallocate($this->Image,$this->BackgroundColorRGB[0],$this->BackgroundColorRGB[1],$this->BackgroundColorRGB[2]); }
再运行代码就能看到灰色的图片code
接下来就要画验证码了,添加一个验证码属性和验证码颜色属性并在构造函数里初始化
private $Captcha,$CaptchaRGB=[255,100,37]; public function __construct($Captcha) { $this->Captcha=$Captcha; $this->CreateImage(); $this->CaptchaRGB=imagecolorallocate($this->Image,$this->CaptchaRGB[0],$this->CaptchaRGB[1],$this->CaptchaRGB[2]); }
还要指定一个字体,并画验证码
private $Font='C:'.DIRECTORY_SEPARATOR.'Windows'.DIRECTORY_SEPARATOR.'Fonts'.DIRECTORY_SEPARATOR.'simhei.ttf'; private function DrawCaptcha(){ //参数:图片,字体大小,角度(彷佛是0-360),x坐标,y坐标,验证码颜色,字体,验证码 imagettftext($this->Image,20,0,10,30,$this->CaptchaRGB,$this->Font,$this->Captcha); }
在生成验证码方法进行调用,并在实例化时传入验证码
public function Generate() { $this->DrawCaptcha(); $this->Output(); } //验证码类无论验证码字符串的生成,验证 $gcudCaptcha=new \gcud\gcudCaptcha('123456'); $gcudCaptcha->Generate();
再运行一遍就能看橙色的123456出现,仔细看还能看到花花绿绿的失真现象,不想看到这种状况能够把imagejpeg换成imagepng函数,header类型也能够换下,但其实除了下载图片的后缀名浏览器同样能正确解析
到这里其实算是实现了验证码,但想起验证码的初衷,为了防止被快速破[这也和谐]解就必须加大难度
先看看目前的全部代码
<?php namespace gcud; class gcudCaptcha { private $Weight = 150, $Height = 50, $Image; private $BackgroundColorRGB=[127,127,127],$CaptchaRGB=[255,100,37]; private $Captcha,$Font='C:'.DIRECTORY_SEPARATOR.'Windows'.DIRECTORY_SEPARATOR.'Fonts'.DIRECTORY_SEPARATOR.'simhei.ttf'; public function __construct($Captcha) { $this->Captcha=$Captcha; $this->CreateImage(); $this->CaptchaRGB=imagecolorallocate($this->Image,$this->CaptchaRGB[0],$this->CaptchaRGB[1],$this->CaptchaRGB[2]); } private function CreateImage() { $this->Image = imagecreate($this->Weight, $this->Height); imagecolorallocate($this->Image,$this->BackgroundColorRGB[0],$this->BackgroundColorRGB[1],$this->BackgroundColorRGB[2]); } private function Output() { header('Content-Type:image/png'); imagepng($this->Image); imagedestroy($this->Image); } private function DrawCaptcha(){ imagettftext($this->Image,20,0,10,30,$this->CaptchaRGB,$this->Font,$this->Captcha); } public function Generate() { $this->DrawCaptcha(); $this->Output(); } }
因此第一个方法是把每一个字符随机旋转绘制,因而绘制方法更改以下
private function DrawCaptcha(){ $Length=strlen($this->Captcha); $X=0; for ($I=0;$I<$Length;$I++){ imagettftext($this->Image,20,mt_rand(0,360),$X,30,$this->CaptchaRGB,$this->Font,$this->Captcha[$I]); $X+=20; } }
再次运行就能看到东倒西歪的验证码,但仅仅如此还不够,还须要把其它几个参数利用起来,能够尝试随机的字体大小,也能够使用不一样字体,不一样颜色,用中文作验证码等等
再写个常见的画线条,先加个属性$LineRGB=[37,32,98]并在构造函数里初始化,而后把画线条的下在画验证码那里
private function DrawCaptcha(){ $Length=strlen($this->Captcha); $X=0; for ($I=0;$I<$Length;$I++){ imagettftext($this->Image,mt_rand(10,20),mt_rand(0,360),$X,30,$this->CaptchaRGB,$this->Font,$this->Captcha[$I]); //参数图片,x起点,y起点,x终点,y终点,颜色 imageline($this->Image,mt_rand(0,$this->Weight),mt_rand(0,$this->Height),mt_rand(0,$this->Weight),mt_rand(0,$this->Height),$this->LineRGB); $X+=20; } }
更多的就不弄了,有兴趣的本身研究,最后奉上完整代码
<?php namespace gcud; class gcudCaptcha { private $Weight = 150, $Height = 50, $Image; private $BackgroundColorRGB=[127,127,127],$CaptchaRGB=[255,100,37],$LineRGB=[37,32,98]; private $Captcha,$Font='C:'.DIRECTORY_SEPARATOR.'Windows'.DIRECTORY_SEPARATOR.'Fonts'.DIRECTORY_SEPARATOR.'simhei.ttf'; public function __construct($Captcha) { $this->Captcha=$Captcha; $this->CreateImage(); $this->CaptchaRGB=imagecolorallocate($this->Image,$this->CaptchaRGB[0],$this->CaptchaRGB[1],$this->CaptchaRGB[2]); $this->LineRGB=imagecolorallocate($this->Image,$this->LineRGB[0],$this->LineRGB[1],$this->LineRGB[2]); } private function CreateImage() { $this->Image = imagecreate($this->Weight, $this->Height); imagecolorallocate($this->Image,$this->BackgroundColorRGB[0],$this->BackgroundColorRGB[1],$this->BackgroundColorRGB[2]); } private function Output() { header('Content-Type:image/png'); imagepng($this->Image); imagedestroy($this->Image); } private function DrawCaptcha(){ $Length=strlen($this->Captcha); $X=0; for ($I=0;$I<$Length;$I++){ imagettftext($this->Image,mt_rand(10,20),mt_rand(0,360),$X,30,$this->CaptchaRGB,$this->Font,$this->Captcha[$I]); imageline($this->Image,mt_rand(0,$this->Weight),mt_rand(0,$this->Height),mt_rand(0,$this->Weight),mt_rand(0,$this->Height),$this->LineRGB); $X+=20; } } public function Generate() { $this->DrawCaptcha(); $this->Output(); } }