GD实战开发验证码

时间:2022-05-03
本文章向大家介绍GD实战开发验证码,主要内容包括GD、根据需求写方法、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

GD

GD库是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,在网站上GD库通常用来生成缩略图,或者用来对图片加水印,或者用来生成汉字验证码,或者对网站数据生成报表等;

今天benny带领大家学习并分享一个生成验证码类。

设置Captcha类属性

作为一个普通的验证码,它由以下几个方面组成:

验证码:public $checkcode; //产生的验证码验证码图片:private $checkimage; //验证码图片 图片的宽高和验证码个数:private $width=80,$height=20,$codenum=4;(默认取值) 干扰元素: private $disturbColor = ''; //干扰像素 出于安全性考虑的session:private $session_flag;

相关属性设置完后,得:

class Captcha
{
  private $width=80,$height=20,$codenum=4;
  public $checkcode; //产生的验证码
  private $checkimage; //验证码图片
  private $disturbColor = ''; //干扰像素
  private $session_flag='captcha_code';//存到session中的索引
//尝试开始session
  function __construct(){
    @session_start();
  }
 function __destruct
  {
    unset($this->width,$this->height,$this->codenum,$this->session_flag);
  }
}

PS: construct()和destruct()为构造函数和析构函数。

根据需求写方法

生成验证码

(如未显示全,可右滑)
private function createCode()
  {
    $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum));
  }

substr() 函数返回字符串的一部分。 详细查看:http://www.w3school.com.cn/php/funcstringsubstr.asp

产生验证码图片

(如未显示全,可右滑)
private function createImage()
  {
    $this->checkimage = @imagecreate($this->width,$this->height);
    $back = imagecolorallocate($this->checkimage,255,255,255);
    $border = imagecolorallocate($this->checkimage,0,0,0);
    imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底
    imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border); // 黑色边框
  }

imagecreate:新建一个基于调色板的图像 imagecolorallocate:为一幅图像分配颜色 imagefilledrectangle:画一矩形并填充 imagerectangle: 画一个单一像素

(如未显示全,可右滑)
PS:这些函数可以感觉英语单词来理解它的功能:如image+filled+rectangle = imagesfillledrectangle
详细查看:http://php.net/manual/zh/book.image.php

设置图片的干扰像素

(如未显示全,可右滑)
private function setDisturbColor()
  {
    for ($i=0;$i<=200;$i++)
    {
      $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255));
      imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor);
    }
  }

PS:干扰元素其实就是随机在一个区域内画上不同颜色的点,但是视觉上不影响验证码显示

画上验证码

(如未显示全,可右滑)
private function writeCheckCodeToImage()
  {
    for ($i=0;$i<$this->codenum;$i++)
    {
      $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255));
      $x = floor($this->width/$this->codenum)*$i;
      $y = rand(0,$this->height-15);
      imagechar ($this->checkimage, rand(5,8), $x+5, $y, $this->checkcode[$i], $bg_color);
    }
  }

imagechar:水平地画一个字符

统一调用

(如未显示全,可右滑)
private function outFileHeader()
  {
    header ("Content-type: image/png");
  }
 function create()
  {
//输出头
    $this->outFileHeader();
//产生验证码
    $this->createCode();
//产生图片
    $this->createImage();
//设置干扰像素
    $this->setDisturbColor();
//往图片上写验证码
    $this->writeCheckCodeToImage();
    imagepng($this->checkimage);
    imagedestroy($this->checkimage);
    $_SESSION[$this->session_flag]=$this->checkcode;
  }

imagpng:将PNG图像输出到浏览器或文件

在实际的开发过程中,我们往往会根据不同的需求作出不一样的验证码,这时候我们会设置一个函数,来接受不一样的参数,来完成特定的需求。

接受参数

(如未显示全,可右滑)
function config($width='80',$height='20',$codenum='4',$session_flag='captcha_code')
  {
    $this->width=$width;
    $this->height=$height;
    $this->codenum=$codenum;
    $this->session_flag=$session_flag;
  }

类与方法的调用

(如未显示全,可右滑)
<?php
include "captcha.php";
    $image=new Captcha();
$image->config('80','20','4','');
$image->create();//这样就会向浏览器输出一张图片

效果展示

在这里,小编对代码进行了部分注释,希望对大家的学习有所帮助,也希望有问题加我微信(wzc88czw)交流学习心得。