php之验证码
实现步骤
1.关键函数
imagecreatetruecolor(),
imagefilledrectangle(),
imagettftext()
imagesetpixel()
imageline()
imagecolorallocate()
http://php.net/manual/zh/2.实现步骤
1.imagecreatetruecolor–创建一定宽高度的真彩色图像,获取图像标识符
2.imagefilledrectangle(),矩形填充
3.获取一定格式的字符(验证码显示的数字或者字母)
4.$_SESSION()存储
5.imagettftext()–将字符写入图像
6.imagesetpixel()–设置干扰元素点
7.imageline()–设置干扰元素线
8.输出验证码
9.销毁资源
使用
//得自己设置字体的地址
require 'Captcha.php';
$captcha = new Captcha();
$captcha->font_path = __DIR__ . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR . 'arial.ttf';
$captcha->create();
代码
<?php
/**
* Date: 2016/12/23 16:41
*/
class Captcha
{
private $_link;
private $_height;
private $_width;
private $_session_name;
private $_font_path;
public function __construct(int $width = 80, int $height = 30, string $session_name = 'captcha', string $font_path = '')
{
$this->_width = $width;
$this->_height = $height;
$this->_session_name = $session_name;
$this->_font_path = $font_path ?: $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR;
$this->_link = imagecreatetruecolor($this->_width, $this->_height);
}
public function __get($name)
{
$name = '_' . $name;
return $this->$name ?: "没有该配置选项";
}
public function __set($name, $value)
{
$name = '_' . $name;
$this->$name = $value;
}
public function create(int $type = 1, int $len = 4, int $pixels = 20, int $lines = 5)
{
if (!session_start()) {
session_start();
}
imagefilledrectangle($this->_link, 0, 0, $this->_width - 2, $this->_height - 2, $this->getColor('white'));
$chars = $this->getCaptchaChars($type, $len);
$_SESSION[$this->_session_name] = $chars;
for ($i = 0; $i < mb_strlen($chars, 'UTF-8'); $i++) {
$font_size = mt_rand(14, 18);
$angle = mt_rand(-15, 15);
$x = ($this->_width - 2) / 4 * $i + 2;
$y = $this->_height - 2;
$text = mb_substr($chars, $i, 1, 'UTF-8');
$color = $this->getColor('deep');
imagettftext($this->_link, $font_size, $angle, $x, $y, $color, $this->_font_path, $text);
}
$this->setPixel($pixels);
$this->setLine($lines);
header('Content-type:image/gif');
imagegif($this->_link);
imagedestroy($this->_link);
}
public function setPixel(int $pixels)
{
for ($i = 0; $i < $pixels; $i++) {
imagesetpixel($this->_link, mt_rand(1, $this->_width), mt_rand(1, $this->_height), $this->getColor('deep'));
}
}
public function setLine(int $lines)
{
for ($i = 0; $i < $lines; $i++) {
$x = mt_rand(1, $this->_width - 2);
$y = mt_rand(1, $this->_height - 2);
$angle = mt_rand(-180, 180);
$angle = deg2rad($angle);
$end_x = $x + cos($angle) * $this->_width;
$end_y = $y - sin($angle) * $this->_height;
imageline($this->_link, $x, $y, $end_x, $end_y, $this->getColor('deep'));
}
}
public function getColor(string $type = 'random')
{
if ('white' === $type) {
$r = $g = $b = 255;
} elseif ('black' === $type) {
$r = $g = $b = 0;
} elseif ('deep' === $type) {
$r = mt_rand(0, 100);
$g = mt_rand(0, 180);
$b = mt_rand(0, 200);
} else {
$r = $g = $b = mt_rand(0, 255);
}
return imagecolorallocate($this->_link, $r, $g, $b);
}
public function getCaptchaChars(int $type = 1, int $len = 4): string
{
$chars = '';
if (1 === $type) {
$chars = implode('', range(0, 9));
} elseif (2 === $type) {
$chars = implode('', array_merge(range('A', 'Z'), range('a', 'z')));
} elseif (3 === $type) {
$chars = implode('', array_merge(range('A', 'Z'), range('a', 'z'), range(0, '9')));
}
if ($len > mb_strlen($chars, 'UTF-8')) {
return "验证码字符数超过限制";
}
$chars = mb_substr(str_shuffle($chars), 0, $len, 'UTF-8');
return $chars;
}
public function check($value)
{
return (isset($_SESSION[$this->_session_name]) && $value === $_SESSION[$this->_session_name]) ?: false;
}
}