php使用GD库生成验证码
2017-01-03 17:53
423 查看
目录
绘图步骤绘制验证码步骤
代码演示
一、绘图步骤:
1.创建画布,分配颜色,使用以下两个函数(可以在php手册GD库函数中找到):imagecreatetruecolor()
imagecolorallocate()
2.绘画过程:
imagefill()
imagettftext()
imagesetpixel()
imageline()
3.输出图像:
header(“Content-Type:image/png”);
imagepng();
imagejpeg();
4.销毁图片(释放内存)
imagedestroy();
二、绘图具体步骤
生成验证码字符串private function getCode(){ $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $t=''; for($i=0;$i<$this->m;$i++){ $t.=$str[rand(0,$this->type['letter'])]; } return $t; }
画出验证码
function drawCode(){ $code=$this->getCode();//获取验证码字符串 imagefill($this->im, 0, 0, $this->bg); for ($i=0; $i <200 ; $i++) { imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255)); } imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255)); $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200)); for($i=0;$i<$this->m;$i++){ imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] ); } }
输出图像
function printImage(){ $this->drawCode(); header("Content-Type:image/jpeg"); imagepng($this->im); $this->destroy(); } function destroy(){ imagedestroy($this->im); }
三、代码演示
<?php
/**
* 验证码画图类
*/
class Image
{
private $width;
private $height;
private $im;
private $bg;
private $m;
private $type=array('num' => 9,'letter' => 61 );
function __construct($width,$height,$m)
{
$this->width=$width;
$this->height=$height;
$this->m=$m;
$this->im=imagecreatetruecolor($this->width, $this->height);
$this->bg=imagecolorallocate($this->im, 220, 220, 220);
}
private function getCode(){ $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $t=''; for($i=0;$i<$this->m;$i++){ $t.=$str[rand(0,$this->type['letter'])]; } return $t; }
function drawCode(){
$code=$this->getCode();//获取验证码字符串
imagefill($this->im, 0, 0, $this->bg);
//加干扰点
for ($i=0; $i <200 ; $i++) {
imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255));
}
//加干扰线
imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255));
$c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200));
for($i=0;$i<$this->m;$i++){
imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] );
}
}
function printImage(){ $this->drawCode(); header("Content-Type:image/jpeg"); imagepng($this->im); $this->destroy(); } function destroy(){ imagedestroy($this->im); }
}
function unit_test(){
$obj=new Image(20*4,30,4);
$obj->printImage();
}
unit_test();
?>
相关文章推荐
- PHP 使用GD库生成验证码 在图像上绘制汉字
- PHP使用GD库生成图像验证码
- PHP生成图像验证码(GD库的使用)
- php 使用GD库生成验证码
- 关于PHP使用GD库生成的验证码无法在别处显示
- php 使用GD库生成验证码
- 使用Lua GD库动态生成验证码图片
- php验证码 GD库生成验证码
- PHP GD库生成验证码
- php中使用GD库做验证码
- 使用Lua GD库动态生成验证码图片(2)
- 一分钟学会使用PHP生成网页验证码
- PHP使用GD库画简单的验证码
- php学习笔记:利用gd库生成图片,并实现随机验证码
- php生成缩略图示例代码分享(使用gd库实现)
- [PHP]GD库生成图像无法在浏览器正常显示的问题(更新:使用GD图像生成类发现错误必须及时清理缓存!)
- 如何运用PHP GD库生成验证码
- php 验证码生成方法 及使用
- PHP使用GIFEncoder类生成的GIF动态图片验证码
- PHP 用session与gd库实现简单验证码生成与验证的类