关于php:php实现自动生成验证码的实例讲解

4次阅读

共计 3655 个字符,预计需要花费 10 分钟才能阅读完成。

在本篇文章里小编给大家整顿了一篇对于 php 实现主动生成验证码的实例解说内容,有须要的敌人们能够跟着学习下。

当初验证码在表单中的利用越来越多了,然而如果用 js 来实现总感觉不太不便,因而应用 php 来实现下,在此记录下。

当然,咱们也能够封装成一个函数,当前应用的时候也是很不便的,这里并未封装,感兴趣的小伙伴能够本人封装下。

具体实现代码:

新建一个 cap_sz.php 文件:

<?php
session_start(); // 设置 session, 肯定要在顶部
$width = 150; // 设置图片宽为 300 像素
$height = 40; // 设置图片高为 40 像素
$image = imagecreatetruecolor($width, $height); // 设置验证码大小的函数
$bgcolor = imagecolorallocate($image, 255, 255, 255); // 验证码色彩 RGB 为 (255,255,255)#ffffff
imagefill($image, 0, 0, $bgcolor); // 区域填充
$cap_code = "";
for($i=0;$i<4;$i++){
    $fontsize = 7; // 设置字体大小
    $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    // 数字越大,色彩越浅,这里是深色彩 0 -120
    $fontcontent = rand(0,9);
    $cap_code.=$fontcontent; //.= 间断定义变量
    $x = ($i*150/4)+rand(5,10);
    $y = rand(5,10);
    // 设置坐标
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
$_SESSION['code'] = $cap_code; // 存到 session
// 设置烦扰元素,设置雪花点
for($i=0;$i<300;$i++){$inputcolor = imagecolorallocate($image, rand(50,200), rand(20,200), rand(50,200));
    // 设置色彩,20-200 色彩比数字浅,不烦扰浏览
    imagesetpixel($image, rand(1,149), rand(1,39), $inputcolor);
    // 画一个繁多像素的元素
}
// 减少烦扰元素,设置横线 (先设置线的色彩,在设置横线)
for ($i=0;$i<4;$i++) {$linecolor = imagecolorallocate($image, rand(20,220), rand(20,220),rand(20,220));
    // 设置线的色彩
    imageline($image, rand(1,149), rand(1,39), rand(1,299), rand(1,149), $linecolor);
 
}
 
// 因为有些浏览器,拜访的 content-type 会是文本型(乱码),所以咱们须要设置成图片的格局类型
header('Content-Type:image/png');
imagepng($image); // 建设 png 函数
imagedestroy($image); // 完结图形函数,打消 $image

实例扩大:

<?php
$iC = new idCode(5,60,30);
$iC->createPNG();

class idCode{
  private $words = array('a','b',
  'c','d','e','f','g','h','i','j','k','l',
  'm','n','o','p','q','r','s','t','u','v',
  'w','x','y','z','A','B','C','D','E','F',
  'G','H','I','J','K','L','M','N','O','P',
  'Q','R','S','T','U','V','W','X','Y','Z',
  '0','1','2','3','4','5','6','7','8','9');
  private $fonts;
  private $count;// 验证码字符数
  private $height;
  private $width;
  private $path = '..\myfolder\fonts';
  private $keys;

  // 构造函数
  public function __construct($count,$width,$height){
    $this->count = $count;
    $this->getFonts();
    $this->height = $height;
    $this->width = $width;
  }

  private function getFonts(){$dir = dir($this->path);

    while(false !== ($file = $dir->read())){if($file != '.' && $file != '..'){$this->fonts[count($this->fonts)] = basename($file);
        }
    }
    $dir->close();}

  private function createKeys(){for($i = 0;$i < $this->count;$i++){$this->keys[$i]['char'] = $this->words[rand(0,count($this->words)-1)];
      // 应用字体门路标识
      $this->keys[$i]['filename'] = $this->path.'\\'.$this->fonts[rand(0,count($this->fonts)-1)];
    }
  }

  public function createPNG(){$this->createKeys();

    // 创立画布以及色彩块儿
    $bg = imagecreatetruecolor($this->width + 10*2,$this->height + 3*2);// 两边留 10px 空白, 高低 3px
    $grey = imagecolorallocate($bg,155,155,155);
    $blue = imagecolorallocate($bg,0x00,0x00,0xff);
    // 填充背景
    imagefill($bg,0,0,$grey);
    // 增加字符
    $pwidth = $this->width/$this->count;
    $x;$y;
    for($i = 0;$i < $this->count;$i++){$rotation = rand(-40,40);// 偏转角度±40°
      $fontsize = 33;
      $width_txt;
      $height_txt;

      do{
        $fontsize--;
        $bbox = imagettfbbox($fontsize,$rotation,$this->keys[$i]['filename'],$this->keys[$i]['char']);
        $width_txt = $bbox[2] - $bbox[0];//x 0 2 4 6,y1 3 5 7; 左下,右下,右上,左上
        $height_txt = $bbox[7] - $bbox[1];
      }while($fontsize > 8 && ($height_txt > $this->height || $width_txt > $pwidth));

      $fontcolor = imagecolorallocate($bg,rand(0,255),rand(0,255),rand(0,255));
      $x = 8 + $pwidth*$i + $pwidth/2 - $width_txt/2;// x 坐标根本地位
      $y = $this->height/2 - $height_txt/2;

      imagettftext($bg,$fontsize,$rotation,$x,$y,$fontcolor,$this->keys[$i]['filename'],$this->keys[$i]['char']);
    }
    // 绘制烦扰线
    // 依据字体酌情减少烦扰线
    imageline($bg,0,15,40,10,$blue);
    // 图像输入头文件
    header('Content-type:image/png');
    // 输入 png 图像
    imagepng($bg);
    // 革除缓存资源
    imagedestroy($bg);
  }

  public function checkKeys($input){if(count($input)!=$this->count){return 'ERROR: 长度不正确.';}else{for($i=0;$i < $this->count;$i++){
        //0 o O I l 1 校准, 依据所抉择的字体确定是否须要手动校准
        if($input[$i] != $this->keys[$i]['char']){return 'SUCCESS.';}else{return 'ERROR: 请输出正确验证码.';}
      }
    }
  }
}
?>
正文完
 0