GD库
1、Php中gd库的使用
Gd库是一个画图或处理有图片的函数库
2、使用gd库画图
GD库图像绘制的步骤
在PHP中创建一个图像应该完成如下所示的4个步骤:
1.创建一个背景图像(也叫画布),以后的操作都基于此背景图像。
2.在背景上绘制图像轮廓或输入文本。
3.输出最终图形
4.释放资源
代码:
<?php
//1. 创建画布
$im = imageCreateTrueColor(200, 200); //建立空白画布背景
$white = imageColorAllocate($im, 255, 255, 255); //设置画布颜色
$blue = imageColorAllocate ($im, 0, 0, 64);
//2. 开始绘画
imageFill($im, 0, 0, $blue); //绘制背景
imageLine($im, 0, 0, 200, 200, $white); //画线
imageString($im, 4, 50, 150, ‘Sales‘, $white); //添加字串
//3. 输出图像
header(‘Content-type: image/png‘);
imagePng ($im); //以 PNG 格式将图像输出
//4. 释放资源
imageDestroy($im);
画布管理
imagecreate -- 新建一个基于调色板的图像
– resource imagecreate ( int x_size, int y_size )
–本函数用来建立空新画布,参数为图片大小,单位为像素 (pixel)。支持256色。
imagecreatetruecolor -- 新建一个真彩色图像
– resource imagecreatetruecolor ( int x_size, int y_size )
–新建一个真彩色图像画布 ,需要 GD 2.0.1 或更高版本,不能用于 GIF 文件格式。
imagedestroy -- 销毁一图像
– bool imagedestroy ( resource image )
– imagedestroy() 释放与 image 关联的内存
设置颜色
imagecolorallocate -- 为一幅图像分配颜色
–语法:int imagecolorallocate ( resourceimage, int red,int green, int blue )
imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成
分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,
绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00
到 0xFF。imagecolorallocate() 必须被调用以创建每一种用在
image 所代表的图像中的颜色。
$im =imagecreatetruecolor(100, 100); //创建画布的大小为100x100
$red =imagecolorallocate($im,255,0,0); //由十进制整数设置一个颜色
$white =imagecolorallocate($im, 0xFF, 0xFF, 0xFF);// 十六进制方式
生成图片
imagegif -- 以 GIF 格式将图像输出到浏览器或文件
–语法:bool imagegif (resource image[,string filename] )
imagejpeg -- 以 JPEG 格式将图像输出到浏览器或文件
–语法:bool imagejpeg (resource image [,stringfilename [, int quality]])
imagepng -- 以 PNG 格式将图像输出到浏览器或文件
–语法:bool imagepng (resource image[,string filename] )
imagewbmp -- 以 WBMP 格式将图像输出到浏览器或文件
–语法:bool imagewbmp (resource image [,string filename [, intforeground]] )
3、画各种图形
imagefill -- 区域填充
–语法:bool imagefill(resource image,intx,int y, int color)
– imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
imagesetpixel -- 画一个单一像素
–语法:bool imagesetpixel ( resourceimage, int x, int y, int color )
– imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
imageline -- 画一条线段
–语法:bool imageline ( resource image,int x1, int y1, int x2, inty2, int color )
– imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
imagerectangle -- 画一个矩形
–语法:bool imagerectangle ( resourceimage, int x1, int y1,int x2, int y2, int col )
– imagerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。
imagefilledrectangle -- 画一矩形并填充
–语法:bool imagefilledrectangle (resource image, int x1, inty1, int x2, int y2, int color )
– imagefilledrectangle() 在 image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。
imageellipse -- 画一个椭圆
–语法:bool imageellipse ( resourceimage, int cx, int cy, intw, int h, int color )
– imageellipse() 在 image 所代表的图像中画一个中心为 cx,cy(图像左上角为 0, 0)的椭圆。w 和 h 分别指定了椭圆的宽度和高度,椭圆的颜色由 color 指定。
imagefilledellipse -- 画一椭圆并填充
–语法:bool imagefilledellipse ( resourceimage, int cx, intcy, int w, int h, int color )
– imagefilledellipse() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)为中心画一个椭圆。w 和 h 分别指定了椭圆的宽和高。椭圆用 color 颜色填充。如果成功则返回
TRUE,失败则返回 FALSE。
imagearc -- 画椭圆弧
– bool imagearc ( resource image, int cx, int cy, int w, int h, int s,inte, int color )
– imagearc() 以 cx,cy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。w 和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。
imagefilledarc -- 画一椭圆弧且填充
– bool imagefilledarc ( resource image, int cx, int cy, int w, int h,ints, int e, int color, int style )
– imagefilledarc() 在 image 所代表的图像中以 cx,cy(图像左上角为 0, 0)画一椭圆弧。如果成功则返回 TRUE,失败则返回 FALSE。w 和 h 分别指定了椭圆的宽和高,s 和 e 参数以角度指定了起始和结束点。style 可以是下列值按位或(OR)后的值:
– IMG_ARC_PIE IMG_ARC_CHORD
– IMG_ARC_NOFILL IMG_ARC_EDGED
imagestring -- 水平地画一行字符串
–语法:bool imagestring ( resource image,int font, int x, int y,string s, int col )
– imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
imagestringup -- 垂直地画一行字符串
–语法:bool imagestringup ( resourceimage, int font, int x, inty, string s, int col )
– imagestring()用 col 颜色将字符串 s 垂直地画到 image 所代表的图像的 x , y 座标处(图像的左上角为 0, 0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
imagechar -- 水平地画一个字符
–语法:bool imagechar ( resource image,int font, int x, int y, string c,int color )
– imagechar() 将字符串 c 的第一个字符画在 image 指定的图像中,其左上角位于 x,y(图像左上角为 0, 0),颜色为 color。如果 font 是1,2,3,4 或 5,则使用内置的字体(更大的数字对应于更大的字体)。
imagecharup -- 垂直地画一个字符
–语法:bool imagecharup ( resource image,int font, int x, int y, stringc, int color )
– imagecharup() 将字符 c 垂直地画在 image 指定的图像上,位于 x,y(图像左上角为 0, 0),颜色为 color。如果 font 为 1,2,3,4 或 5,则使用内置的字体。
imagettftext -- 用 TrueType 字体向图像写入文本
–语法 :array imagettftext ( resource image, floatsize, float angle, intx, int y, int color, string fontfile, string text )
代码:
//1创建资源(画布的大小)
$img = imagecreatetruecolor(200, 200);
//设置画布的颜色
$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
$red =imagecolorallocate($img, 255, 0, 0);
$blue =imagecolorallocate($img, 0, 0, 0XFF);
$pink =imagecolorallocate($img, 0XFF, 0, 0XFF);
$green = imagecolorallocate($img, 0, 0xFF, 0);
imagefill($img, 0, 0, $white);//填充画布颜色
2、画各种图形
//画线
imageline($img, 0,0, 200,200, $blue);
imageline($img, 200, 0, 0, 200, $red);
//画矩形
imagerectangle($img, 50, 50, 150, 150,$pink);
imagefilledrectangle($img, 75,75,125,125, $blue);
//画圆
imageellipse($img, 50, 50, 100, 100,$red);
imagefilledellipse($img, 150, 150, 100,100, $red);
//画弧形
imagearc($img, 150, 50, 100, 100, -90, 0, $blue);
//画一个字符串
imagestring($img, 5, 50, 150,"hello world", $blue);
imagestringup($img, 5, 50, 150,"hello world", $blue);
//3. 画出各种图形,和写(画出)字
imagettftext($img, 30, 0, 10, 100,$green, "./simkai.ttf", "妹子漂亮吗?");
imagettftext($img, 30, 0, 13, 103,$red, "./simkai.ttf", "妹子漂亮吗?");
//4保存,或输出给浏览, 写第二个参数就是保存
header("Content-Type:images/gif");
imagegif($img);
//5. 释放资源
imagedestroy($img);
4、时钟代码:
//获取系统时间
date_default_timezone_set("PRC");
$h = date("H");
$i = date("i");
$s = date("s");
//1 创建资源(画布的大小)
$img = imagecreatetruecolor(200, 250);
//设置画布的颜色
$white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
$red =imagecolorallocate($img, 255, 0, 0);
$blue =imagecolorallocate($img, 0, 0, 0XFF);
$pink =imagecolorallocate($img, 0XFF, 0, 0XFF);
$green = imagecolorallocate($img, 0, 0xFF, 0);
imagefill($img, 0, 0, $white);
//2. 制作各种图形
imageellipse($img, 100, 100, 190, 190,$blue);
imagefilledellipse($img, 100, 100, 4,4, $blue);
imagestring($img, 3, 95, 8, "12", $blue);
imagestring($img, 3,180, 95,"03", $blue);
imagestring($img, 3, 95, 180, "06", $blue);
imagestring($img, 3, 11, 95, "09", $blue);
//秒针
$len = 80;
$a = $len*sin(pi()/30*$s);
$b = $len*cos(pi()/30*$s);
$x = 100 + $a;
$y = 100 - $b;
imageline($img, 100, 100, $x, $y,$red);
//数字的时间
imagestring($img, 5, 20, 230,"NOW: {$h}:{$i}:{$s}", $red);
//4保存,或输出给浏览, 写第二个参数就是保存
header("Content-Type:image/gif");
imagegif($img);
//5. 释放资源
imagedestroy($img);
5、验证码类
Reg.php
<?php
session_start();
if(isset($_POST[‘dosubmit‘])){
if(strtoupper($_SESSION[‘code‘]) ==strtoupper($_POST[‘code‘]) ) {
echo "输入成功!<br>";
}else{
echo "输入不对!<br>";
}
}
?>
<body>
<form action="reg.php"method="post">
username: <input type="text"name="username"> <br>
password: <inputtype="password" name="password"> <br>
code: <inputtype="text" onkeyup="if(this.value!=this.value.toUpperCase())this.value=this.value.toUpperCase()" size="6"name="code">
<imgsrc="code.php" + this.src=‘code.php?‘+Math.random()"/> <br>
<inputtype="submit" name="dosubmit" value="登 录"> <br>
</form>
</body>
Code.php
<?php
//开启session
session_start();
include "vcode.class.php";
//构造方法
$vcode = new Vcode(120, 30, 6);
//将验证码放到服务器自己的空间保存一份
$_SESSION[‘code‘] =$vcode->getcode();
//将验证码图片输出
$vcode->outimg();
Vcode.class.php
<?php
class Vcode {
private $width; //宽
private $height; //高
private $num; //数量
private $code; //验证码
private $img; //图像的资源
//构造方法,三个参数
function__construct($width=80, $height=20, $num=4) {
$this->width =$width;
$this->height =$height;
$this->num =$num;
$this->code =$this->createcode(); //调用自己的方法
}
//获取字符的验证码,用于保存在服务器中
function getcode() {
return$this->code;
}
//输出图像
function outimg() {
//创建背景 (颜色, 大小, 边框)
$this->createback();
//画字 (大小, 字体颜色)
$this->outstring();
//干扰元素(点, 线条)
$this->setdisturbcolor();
//输出图像
$this->printimg();
}
//创建背景
privatefunction createback() {
//创建资源
$this->img =imagecreatetruecolor($this->width, $this->height);
//设置随机的背景颜色
$bgcolor = imagecolorallocate($this->img, rand(225,255), rand(225, 255), rand(225, 255));
//设置背景填充
imagefill($this->img, 0, 0, $bgcolor);
//画边框
$bordercolor = imagecolorallocate($this->img, 0, 0, 0);
imagerectangle($this->img, 0,0, $this->width-1, $this->height-1, $bordercolor);
}
//画字
privatefunction outstring() {
for($i=0; $i<$this->num; $i++) {
$color=imagecolorallocate($this->img, rand(0, 128), rand(0, 128), rand(0, 128));
$fontsize=rand(3,5); //字体大小
$x =3+($this->width/$this->num)*$i; //水平位置
$y = rand(0,imagefontheight($fontsize)-3);
//画出每个字符
imagechar($this->img,$fontsize, $x, $y, $this->code{$i}, $color);
}
}
//设置干扰元素
privatefunction setdisturbcolor() {
//加上点数
for($i=0; $i<100; $i++) {
$color=imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->img, rand(1,$this->width-2), rand(1, $this->height-2), $color);
}
//加线条
for($i=0;$i<10; $i++) {
$color=imagecolorallocate($this->img, rand(0, 255), rand(0, 128), rand(0, 255));
imagearc($this->img,rand(-10,$this->width+10), rand(-10, $this->height+10), rand(30, 300), rand(30,300), 55,44, $color);
}
}
//输出图像
private function printimg() {
if (imagetypes()& IMG_GIF) {
header("Content-type: image/gif");
imagegif($this->img);
} elseif(function_exists("imagejpeg")) {
header("Content-type: image/jpeg");
imagegif($this->img);
} elseif(imagetypes() & IMG_PNG) {
header("Content-type: image/png");
imagegif($this->img);
} else {
die("No image support in this PHPserver");
}
}
//生成验证码字符串
private function createcode(){
$codes ="3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY";
$code ="";
for($i=0; $i <$this->num; $i++) {
$code.=$codes{rand(0, strlen($codes)-1)};
}
return $code;
}
//用于自动销毁图像资源
function __destruct() {
imagedestroy($this->img);
}
}
6、Php图片处理
图片背景管理
图片缩放和裁剪
添加图片水印
图片旋转和翻转
图片背景管理
从指定的图片文件或 URL地址来新建一个图像。成功则返回一个图像标识符,失败时返回一个空字符串,并且输出一条错误信息。由于格式不同,则需要分别使用对应图片背景处理函数。
–resource imagecreatefrompng ( string filename ) 从 PNG 文件或 URL 新建一图像
–resource imagecreatefromjpeg ( string filename ) 从 JPEG 文件或 URL 新建一图像
–resource imagecreatefromgif ( string filename ) 从 GIF 文件或 URL 新建一图像
–resource imagecreatefromwbmp ( string filename ) 从 WBMP 文件或 URL 新建一图像
其他图像处理函数:
– intimagesx ( resource image ) 取得图像宽度
– intimagesy ( resource image ) 取得图像高度
– arraygetimagesize ( string $filename [, array &$imageinfo ] ) 取得图像大小、类型等信息
代码:
<?php
function cimgstr($imgname, $string) {
list($width, $height, $type)=getimagesize($imgname);
$types =array(1=>"gif", 2=>"jpeg", 3=>"png");
//变量函数
$createimage ="imagecreatefrom".$types[$type];
$img =$createimage($imgname);
$red = imagecolorallocate($img, 0xFF, 0, 0);
$x =($width-imagefontwidth(5)*strlen($string))/2;
$y =($height-imagefontheight(5))/2;
imagestring($img, 5, $x, $y,$string, $red);
//变量函数
$save ="image".$types[$type];
$save($img,"new_".$imgname);
imagedestroy($img);
}
cimgstr("dx.jpg","meizi");
cimgstr("map.gif","meizi");
cimgstr("cx.png","meize");
7、图片的缩放和剪切
bool imagecopyresampled ( resource $dst_image , resource$src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int$dst_h , int $src_w , int $src_h )
重采样拷贝部分图像并调整大小,是将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。成功时返回 TRUE, 或者在失败时返回 FALSE。其中dst_image 和 src_image 分别是目标图像和源图像的标识符。
缩放代码:
<?php
&n