php 问答验证码,1.php-实现验证码

需要:GD2模块

函数:

1.imagecreatetruecolor(int $width,int $height): 返回图片标识符

2.imagefilledrectangle(resource $image,int $x1,int $y1,int x2,int y2,int $color):画一个矩形并填充

3.imagettftext(resource $image,float $size,float $angle,int $x,$int $y,int $color,string $fontfile, string $text):用TrueType字体向图像写入文本

4.imagecolorallocate(resource $image,int $red,int $green,int $blue) :为图片分配颜色

5.imagesetpixel(resource $image,int $x,int $y, int $color):画一个单一像素

6.imageline(resource $image,int $x1,int $y1,int $x2,int $y2,int $color):画一条线段

StringFactory:生产验证码所需字符串

class StringFactory{

//生成验证码需要的字符串

public static function verityString($type = 1, $len = 4)

{

if (1 === $type) {

$chars = implode(range(0, 9));

} elseif (2 === $type) {

$chars = implode(array_merge(range('a', 'z'), range('A', 'Z')));

} else {

$chars = implode(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));

} if ($len > strlen($chars)) {

return '字符串长度超出限制';

}

$chars = mb_substr(str_shuffle($chars), 0, $len);

return $chars;

}

}

class Verify{

private $link;

private $width;

private $height;

public function __construct($width = 80, $height = 30) {

$this->link = imagecreatetruecolor($width, $height);

$this->width = $width; $this->height = $height;

}

public function getVerify($fontPath, $sessionName = 'verifyCodeVal', $type = 1, $len = 4, $pixel = 50, $line = 5) {

if (!isset($_SESSION)) {

session_start();

}

imagefilledrectangle($this->link, 1, 1, $this->width - 2, $this->height - 2, $this->getColor('white')); $chars = StringFactory::verityString($type, $len); $_SESSION[$sessionName] = $chars; for ($i = 0; $i < $len; $i++) { $fontSize = mt_rand(14, 18); $angle = mt_rand(-15, 15); $x = ($this->width - 2) * $i / 4 + 2; $y = mt_rand(20, $this->height - 1); $color = $this->getColor('dark'); $text = mb_substr($chars, $i, 1, 'utf-8'); imagettftext($this->link, $fontSize, $angle, $x, $y, $color, $fontPath, $text); } $this->setPixel($pixel); $this->setLine($line); header('content-type:images/gif'); imagegif($this->link); imagedestroy($this->link); } public function getColor($type = 'random') { if ('white' === $type) { $r = 255; $g = 255; $b = 255; } elseif ('black' === $type) { $r = 0; $g = 0; $b = 0; } elseif ('dark' === $type) { $r = mt_rand(0, 100); $g = mt_rand(0, 180); $b = mt_rand(0, 200); } else { $r = mt_rand(0, 255); $g = mt_rand(0, 255); $b = mt_rand(0, 255); } $color = imagecolorallocate($this->link, $r, $g, $b); return $color; } public function setPixel($count) { for ($i = 0; $i < $count; $i++) { imagesetpixel($this->link, mt_rand(1, $this->width - 1), mt_rand(1, $this->height - 1), $this->getColor('dark')); } } public function setLine($count) { for ($i = 0; $i < $count; $i++) { imageline($this->link, mt_rand(1, $this->width - 1), mt_rand(1, $this->height - 1), mt_rand(1, $this->width - 1), mt_rand(1, $this->height - 1), $this->getColor()); } }}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值