验证码类

<?php
/*-----------------------------------------------------------------------------------
 *类名称 :Verification_code
 *函数功能:设置验证码的大小 字体
 *函数:  5个
 *函数参数说明:设置验证码长宽:set_size;设置验证码字体大小:set_font_size;
 设置验证码字体间距:set_padding;设置验证码字体上下距离:set_line_height; 显示验证码:Watermark
 *说明:必须包含一种字体;

by:subtract
----------------------------------------------------------------------------------*/
class Verification_code
{
     private  $width;
     private  $height;
     private  $font_size;
     private  $padding;
  private  $line_height;
    
  /*初始化*/
 public function Verification_code()
 {
      $this->width = 70;
   $this->height = 25;
   $this->font_size = 15;
   $this->padding = 18;
   $this->line_height=25;
 }
 /*设置验证码长宽*/
 public function set_size($width,$height)
 {
    $this->width=$width;
    $this->height=$height;
 }
 /*设置验证码字体大小*/
 public function set_font_size($font_size)
 {
    $this->font_size=$font_size;
 }
 /*设置验证码字体间距*/
 public function set_padding($padding)
 {
    $this->padding=$padding;
 }
 /*设置验证码字体上下距离*/
 public function set_line_height($line_height)
 {
    $this->line_height=$line_height;
 }
 /*显示验证码*/
    public function Watermark()
    {
        for($i=0;$i<4;$i++){$str[$i]=dechex(rand(0,15));}
        $im = imagecreate($this->width, $this->height);
        $bg = imagecolorallocate($im, 255, 255, 255);
       
        /*设置文字颜色*/
        $textcolor[0] = imagecolorallocate($im, 0, 0, 255);
        $textcolor[1] = imagecolorallocate($im, 255, 0, 0);
        $textcolor[2] = imagecolorallocate($im, 51, 153,0);
        $textcolor[3] = imagecolorallocate($im, 255, 0,255);
        /*设置方格*/       
        $linecolor=imagecolorallocate($im,192,192,192);
        for($j=0;$j<4;$j++){imageline ($im,0,$j*5+5,100,$j*5+5,$linecolor);}
        for($j=0;$j<18;$j++){imageline ($im,$j*5+5,0,$j*5+5,30,$linecolor);}
         
        /*设置点*/
       // for($i=0;$i<300;$i++){imagesetpixel($im,rand(0,80),rand(0,30),$textcolor[0] );}
        for($i=0;$i<4;$i++)
        {
            $x=rand(0,3);
            $str[$i]=iconv("gbk","UTF-8",$str[$i]);
            imagettftext($im,$this->font_size+rand(0,8),0,$i*($this->padding),$this->line_height,$textcolor[$x],"potatpress__.ttf",$str[$i]);
        }
        header("Content-type: image/png");
        imagepng($im);       
    }
}
?>

原文地址:https://www.cnblogs.com/subtract/p/3161396.html