前面的文章已经把准备工作都做好了,我们做了注册页面,配置文件,工具文件等基础功能,接下来就进入短信验证码开发的关键阶段,来带领大家完成验证码图片的制作,以及前台在页面上的调用。
验证码开发主要有这么几个部分:创建一个图片画布,生成随机的验证码,把验证码放到画布上,生成干扰线,最后输出图片。下面上代码:
<?php
class Code{
private $img;
private $width=100;
private $height=30;
private $bgColor='#ffffff';
private $code;
private $codeStr='23456789abcdefghjkmnpqrstuvwsyz';
private $codeLen=4;
private $font;
private $fontSize=16;
private $fontColor='';
public function __construct() {
}
public function make()
{
if(empty($this->font))
{
$this->font = __DIR__ . '/../font/consola.ttf';
}
$this->create();
header("Content-type:image/png");
imagepng($this->img);
imagedestroy($this->img);
exit;
}
public function font($font)
{
$this->font= $font;
return $this;
}
public function fontSize($fontSize)
{
$this->fontSize=$fontSize;
return $this;
}
public function fontColor($fontColor)
{
$this->fontColor = $fontColor;
return $this;
}
public function num($num)
{
$this->codeLen=$num;
return $this;
}
public function width($width)
{
$this->width = $width;
return $this;
}
public function height($height)
{
$this->height = $height;
return $this;
}
public function background($color)
{
$this->bgColor = $color;
return $this;
}
public function get() {
return $_SESSION['code'];
}
private function createCode() {
$code = '';
for ($i = 0; $i < $this->codeLen; $i++) {
$code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)];
}
$this->code = strtoupper($code);
$_SESSION['code'] = $this->code;
}
private function create() {
if (!$this->checkGD())
return false;
$w = $this->width;
$h = $this->height;
$bgColor = $this->bgColor;
$img = imagecreatetruecolor($w, $h);
$bgColor = imagecolorallocate($img, hexdec(substr($bgColor, 1, 2)), hexdec(substr($bgColor, 3, 2)), hexdec(substr($bgColor, 5, 2)));
imagefill($img, 0, 0, $bgColor);
$this->img = $img;
$this->createLine();
$this->createFont();
$this->createPix();
$this->createRec();
}
private function createLine(){
$w = $this->width;
$h = $this->height;
$line_color = "#dcdcdc";
$color = imagecolorallocate($this->img, hexdec(substr($line_color, 1, 2)), hexdec(substr($line_color, 3, 2)), hexdec(substr($line_color, 5, 2)));
$l = $h/5;
for($i=1;$i<$l;$i++){
$step =$i*5;
imageline($this->img, 0, $step, $w,$step, $color);
}
$l= $w/10;
for($i=1;$i<$l;$i++){
$step =$i*10;
imageline($this->img, $step, 0, $step,$h, $color);
}
}
private function createRec() {
}
private function createFont() {
$this->createCode();
$color = $this->fontColor;
if (!empty($color)) {
$fontColor = imagecolorallocate($this->img, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
}
$x = ($this->width - 10) / $this->codeLen;
for ($i = 0; $i < $this->codeLen; $i++) {
if (empty($color)) {
$fontColor = imagecolorallocate($this->img, mt_rand(50, 155), mt_rand(50, 155), mt_rand(50, 155));
}
imagettftext($this->img, $this->fontSize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontColor, $this->font, $this->code [$i]);
}
$this->fontColor = $fontColor;
}
private function createPix() {
$pix_color = $this->fontColor;
for ($i = 0; $i < 50; $i++) {
imagesetpixel($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
}
for ($i = 0; $i < 2; $i++) {
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
}
for ($i = 0; $i < 1; $i++) {
imagearc($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height)
, mt_rand(0, 160), mt_rand(0, 200), $pix_color);
}
imagesetthickness($this->img, 1);
}
private function checkGD() {
return extension_loaded('gd') && function_exists("imagepng");
}
}
好了,这样图形验证码类制作好了。接下来我们来做一个调用文件,用于把图形验证码展示出来。 在sms/tool下创建showCode文件,代码如下:
<?php
session_start();
require('./Code.php');
$code = new Code();
$code->make();
我们还需要完成最后一个步骤,就是在页面上面展示图形验证码。
打开在前面准备工作中创建的注册文件register.php, 找到验证码这里,把代码改成如下:
<div class="form-input">
<label class="code_label">
验 证 码 
<input id="vcode" type="text" name="vcode" value=""/>
<img id="code_img" src="./tool/show_code.php" onclick="this.src='./tool/show_code.php?'+Math.random()" />
<p id="code_err" class="errmsg" style="margin:5px 0 0 88px; color:red;"></p>
</label>
</div>
以上代码我做一下解释:在img标签中,src引入了验证码图片的展示类,把图片直接展示在页面上。添加onclick属性的作用是点击切换图片。完成后在页面上展示的效果如下:
图形验证码制作完毕了,但是有一点要注意,在php.ini配置文件中,必须要打开GD库。