<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2018/10/28
* Time: 20:14
*/
/**
*
*/
class Captcha
{
/**
* @param int $width =80
* @param int $height =80
* @param int $length =4
* @param type 1 - 数字 2-字母 3 - 数字 +字母 4 -汉字 5 - 数字运算
*/
protected $image;
protected $width;
protected $height;
protected $length;
protected $fontFile;
public function __construct()
{
}
/**
* @param int $width
* @param int $height
* @param int $length
* @param $type 1 为数字 2 为字母 3 数字+ 字母 4 为汉子 5 为加法运算
* @param $pixel 0 干扰点 1 干扰线 2 圆弧 3全部
* @arr array 若 $jam=3,$arr = [干扰点,干扰线,圆弧];否则,$jam =2,$arr=
*/
public function captcha($width = 200, $height = 80, $length = 4, $type = 2, $jam = [], $arr = [])
{
// 初始化参数
$this->width = $width;
$this->length = $length;
$this->height = $height;
//创建画布
$this->image = imagecreatetruecolor($this->width, $this->height);
$white = imagecolorallocate($this->image,255,255,255);
imagefilledrectangle($this->image,0,0,$this->width,$this->height,$white);
$this->fontFile = './simfang.ttf';
$this->createCaptcha($jam,$arr,$type);
}
public function createCaptcha($jam,$arr,$type)
{
//创建颜色
for ($i = 0; $i < $this->length; $i++) {
$size = mt_rand(24, 28);
$angle = mt_rand(-15, 15);
$x = 16 + ceil($this->width / $this->length) * $i;
$y = mt_rand($this->height / 3, $this->height - 20);
$text = mb_substr(self::captchaType( 5, $this->length), $i, 1, 'utf-8');
imagettftext($this->image, $size, $angle, $x, $y, self::getRandColor($this->image), $this->fontFile, $text);
//imagestring ($image, $font, $x, $y, $string, $color)
// imagestring($this->image, $size, $x, $y,$text, self::getRandColor($this->image) );
}
$this->jam( ); // 调用干扰
//输出图像到浏览器
header('content-type:image/png;charset=utf-8');
imagepng($this->image);
imagedestroy($this->image);
}
/**
* 添加干扰元素
*/
public function jam($type=[1,2,3], $arr = [100, 2, 2])
{
for($i = 0;$i < 3;$i++){
switch ($type[$i]) {
case 1 :
$this->createJam('setpixel', $arr[$i]);
break;
case 2:
$this->createJam('line', $arr[$i]);
break;
case 3 :
$this->createJam('arc', $arr[$i]);
break;
}
}
}
public function createJam($str, $param)
{
for ($i = 0; $i < $param; $i++) {
if ($str == 'arc') {
imagearc($this->image, mt_rand(0, $this->width / 2), mt_rand(0, $this->height / 2), mt_rand(0, $this->width ), mt_rand(0, $this->height), mt_rand(0, 360), mt_rand(0, 360), self::getRandColor($this->image));
} elseif($str =="setpixel"){
imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), self::getRandColor($this->image));
}else{
imageline($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height),mt_rand(0, $this->width), mt_rand(0, $this->height), self::getRandColor($this->image));
}
}
}
/**
* @param $image
* @return 字体颜色
*/
public static function getRandColor($image)
{
return imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
}
/**
*
* @param $length
* @param type 1 - 数字 2-字母 3 - 数字 +字母 4 -汉字 5 - 数字运算
* return $str 验证码里面的内容
*/
public static function captchaType($type = 1, $length = 4)
{
$str = "";
switch ($type) {
case 1:
$str = join('', array_rand(array_flip(range(2, 9)), $length));
break;
case 2:
$str = join('', array_rand(array_flip(array_merge(range('a', 'z'), range('A', "Z"))), $length));
break;
case 3:
$str = join('', array_rand(array_merge(range(2, 9), array_flip(array_merge(range('a', 'z'), range('A', "Z")))), $length));
break;
case 4:
$char = "就 若 朝 美 首 脑 会 谈 推 迟 至 明 年 年 初 金 正 恩 原 定 于 年 内 ";
$arr = str_split($char);
$arr = array_flip(explode(" ", $char));
$str = join('', array_rand($arr, $length));
break;
case 5:
$num1 = array_rand(range(0, 9), 1);
$num2 = array_rand(range(0, 9), 1);
$str = $num1 . "+" . $num2 . "=";
break;
default:
die("非法操作 !");
}
return $str;
}
}
例子:
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2018/10/28
* Time: 22:24
*/
include 'Captcha.php';
header("content-type:text/html;charset=utf8");
$captcha =new Captcha();
$captcha->captcha();