PHP动态图像处理
PHP中GD库的使用
在PHP中,通过GD库处理图像的操作,都是现在内存中处理,操作完成以后再以文件流的方式,输出到浏览器或保存在服务器的磁盘中。创建一个图象应该包括4个基本步骤:
- 创建画布:画布实际上就是在内存中开辟的一块临时区域,用于存储图像的信息。
- 绘制图像:设置图像的颜色,填充点、线、几何图形、文本等。
- 输出图像:完成绘制后,需要将图像以某种格式保存到服务器指定的文件中,或将图像直接输出到浏览器上显示给用户。但在图像输出之前,一定要使用header()函数发送Content-type通知浏览器。
- 释放资源:图像被输出以后,话不中的内容也不再有用。处于节约系统资源的考虑,需要及时清除画布占用的所有内存资源。
画布管理
可以使使用imagecreate()和imageCreateTrueColor()两个函数插件指定的画布。
imagecreate():新建一个基于调色板的图像。
imagecreatetruecolor():新建一个真彩色图像。
画布的句柄如果不再使用,一定要将这个资源销毁,释放内存与该图像的存储单元。调用imagedestroy($image)函数就可以实现。
设置颜色
imagecolorallocate(resouce $image,int $red,int $green,int $blue);
1 2 3 4 5 6 7 8 9 10 | <?php $im = imagecreate(100, 100); //为设置颜色函数提供一个画布资源 //背景设为红色 $background = imagecolorallocate($im, 255, 0, 0); //第一次调用即为画布设置背景颜色 //设定一些颜色 $white = imagecolorallocate($im, 255, 255, 255); //返回由十进制整数设置为白色的标识符 $black = imagecolorallocate($im, 0, 0, 0); //返回由十进制整数设置为黑色的标识符 //十六进制方式 $white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); //返回由十六进制整数设置为白色的标识符 $black = imagecolorallocate($im, 0x00, 0x00, 0x00); //返回由十六进制整数设置为黑色的标识符 |
生成图像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php if (function_exists("imagegif")){ header("Content-type:image/gif"); imagegif($image); }elseif (function_exists("imagejpeg")){ header("Content-type:image/jpeg"); imagejpeg($image, "", 0.5); }elseif (function_exists("imagepng")){ header("Content-type:imagepng"); imagepng($image); }elseif (function_exists("imagewbmp")){ header("Content-type:imagewbmp"); imagewbmp($image); }else{ die("在PHP服务中,不支持图像"); } |
绘制图像
imageFill()实现区域填充。
1 | imagefill(resource $image,int $x,int $y,int $color); |
1 2 3 4 5 6 7 8 | <?php $image = imagecreatetruecolor(100, 100); $red = imagecolorallocate($image, 255, 0, 0); imagefill($image, 0, 0, $red); header("Content-type:image/png"); imagepng($image); imagedestroy($image); |
- 绘制点:在php中,使用imageSetPixel()函数在画布中绘制一个单一像素的点,并且可以设置点的颜色。
1 | bool imagesetpixel(resource $image, int $x, int $y, int $color); |
- 绘制线:imageline()绘制一条线段。
1 | bool imageline(resource $image, int $x1, int $y1,int $x2, int $y2, int $color); |
- 绘制矩形: imageRectangle(),也可以使用imageFilledRectangle()函数绘制一个矩形并填充。
1 2 | bool imageRectangle(resource $image, int $x1, int $y1,int $x2, int $y2, int $color);bool imageFilledRectangle(resource $image, int $x1, int $y1,int $x2, int $y2, int $color); |
- 绘制多边形: imagepolygon(),imagefilledploygon()。
1 2 | bool imageploygon(resource $image, array $points, int $num_points, int $color);bool imageFilledploygon(resource $image, array $points, int $num_points, int $color); |
- 绘制椭圆:imageEllipse()、imageFilledEllipse()函数绘制一个椭圆并填充。
1 2 | bool imageellipse(resource $image, int $cx, int $cy,int $w, int $h, int $color);bool imagefilledellipse(resource $image, int $cx, int $cy,int $w, int $h, int $color); |
- 绘制弧线:imagearc
1 | bool imagearc(resource $image, int $cx, int $cy,int $w, int $h, int $s, int $e, int $color); |
以($cx.$cy)坐标为中心,$w,$h分别为椭圆的宽度与高度,起始点和结束点以$s,$e参数以角度指定。0度在三点钟位置,以顺时针方向绘画。如果要绘制完整的原型,首先要将参数$w,$h设置为相等的值,然后将起始角度$s指定为0,结束角度$e指定为360。
在图像中绘制文字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php $im = imagecreate(150, 150); //创建一个150*150的画布 $bg = imagecolorallocate($im, 255, 255, 255); //设置画布的背景为白色 $black = imagecolorallocate($im, 0, 0, 0); //设置一个颜色变量为黑色 $string = "staven1234"; //在图像中输出的字符串 imageString($im, 3, 28, 70, $string, $black); //水平将字符串输到图像中 imageStringUp($im, 3, 59, 115, $string, $black); //垂直由下而上输到图像中 for($i=0,$j=strlen($string); $i<strlen($string); $i++,$j--){ //循环单个字符输到图像中 imageChar($im, 3, 10*($i+1), 10*($i+2), $string[$i], $black); //向下倾斜输出每个字符 imageCharUp($im, 3, 10*($i+1), 10*($j+2), $string[$i], $black); //向上倾斜输出每个字符 } header('Content-type: image/png'); //设置输出的头部标识符 imagepng($im); //输出PNG格式的图片 |
imagettftext()输出一种可以缩放的与设备无关的TrueType字体。
$angle是角度制表示的角度,0度为从左向右读的文本,更高数值表示逆时针旋转,90度表示从下往上读的文本。$fontfile表示想要使用的TrueType字体的路径。



浙公网安备 33010602011771号