PHP画图函数:生成图像和处理图像
PHP可以使用GD库和ImageMagick库来生成和处理图像。GD库是一个开源的库,可用于生成和处理JPEG、PNG、GIF、WBMP等图像格式。ImageMagick库提供了更多的图像处理功能,并支持更多的图像格式。下面将分别介绍如何使用这两个库来进行图像生成和处理。
一、GD库
1. 生成图像
使用GD库生成图像非常简单,主要步骤如下:
(1)创建一个空白的图像资源:$im = imagecreatetruecolor($width, $height);
(2)设置颜色和画笔:$bg_color = imagecolorallocate($im, 255, 255, 255); $fg_color = imagecolorallocate($im, 0, 0, 0); imagesetthickness($im, $thickness);
(3)绘制图形:绘制线段:imageline($im, $x1, $y1, $x2, $y2, $color); 绘制矩形:imagerectangle($im, $x1, $y1, $x2, $y2, $color); 绘制圆形:imageellipse($im, $cx, $cy, $rx, $ry, $color); 绘制多边形:imagepolygon($im, $points, count($points)/2, $color);
(4)输出图像:header('Content-type: image/png'); imagepng($im); imagedestroy($im);
下面是一个简单的生成图像的例子,绘制一条蓝色的横线和一条红色的竖线:
$width = 200; $height = 200; $im = imagecreatetruecolor($width, $height); $bg_color = imagecolorallocate($im, 255, 255, 255); $line_color = imagecolorallocate($im, 0, 0, 255); $column_color = imagecolorallocate($im, 255, 0, 0); imagesetthickness($im, 2); imageline($im, 0, $height/2, $width, $height/2, $line_color); imageline($im, $width/2, 0, $width/2, $height, $column_color); header('Content-type: image/png'); imagepng($im); imagedestroy($im);
2. 处理图像
GD库提供了丰富的图像处理函数,可以对图像进行缩放、旋转、合并等操作。下面是一些常用的图像处理函数:
(1)缩放:imagecopyresampled($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $dst_width, $dst_height, $src_width, $src_height);
(2)旋转:imagerotate($src_im, $angle, $bg_color);
(3)合并:imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_width, $src_height);
下面是一个使用GD库处理图像的例子,将一张图片缩小50%并旋转90度:
$src_file = "./test.jpg"; $src_im = imagecreatefromjpeg($src_file); $src_width = imagesx($src_im); $src_height = imagesy($src_im); $dst_width = $src_width/2; $dst_height = $src_height/2; $dst_im = imagecreatetruecolor($dst_width, $dst_height); imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); $rotated_im = imagerotate($dst_im, 90, 0); imagepng($rotated_im, "./test_out.png"); imagedestroy($src_im); imagedestroy($dst_im); imagedestroy($rotated_im);
二、ImageMagick库
ImageMagick库提供了更多的图像处理功能,并且可以支持更多的图像格式。使用ImageMagick库处理图像,主要需要安装ImageMagick工具和PHP的ImageMagick扩展,具体安装方法可以参考官方文档。
1. 生成图像
使用ImageMagick库生成图像也很简单,主要用到的函数是Imagick类的方法,主要步骤如下:
(1)创建一个空白的图像资源:$im = new Imagick(); $im->newImage($width, $height, new ImagickPixel('#ffffff'));
(2)设置颜色和画笔:$bg_color = new ImagickPixel('#ffffff'); $fg_color = new ImagickPixel('#000000'); $draw = new ImagickDraw(); $draw->setFillColor($fg_color); $draw->setStrokeColor($fg_color); $draw->setStrokeWidth($thickness);
(3)绘制图形:绘制线段:$draw->line($x1, $y1, $x2, $y2); 绘制矩形:$draw->rectangle($x1, $y1, $x2, $y2); 绘制圆形:$draw->circle($cx, $cy, $cx+$rx, $cy); 绘制多边形:$draw->polygon($points);
(4)输出图像:$im->drawImage($draw); $im->setImageFormat('png'); header('Content-type: image/png'); echo $im; $im->destroy();
下面是一个简单的使用ImageMagick库生成图像的例子,绘制一条蓝色的横线和一条红色的竖线:
$width = 200; $height = 200; $im = new Imagick(); $im->newImage($width, $height, new ImagickPixel('#ffffff')); $line_color = new ImagickPixel('#0000ff'); $column_color = new ImagickPixel('#ff0000'); $draw = new ImagickDraw(); $draw->setFillColor($line_color); $draw->setStrokeColor($line_color); $draw->setStrokeWidth(2); $draw->line(0, $height/2, $width, $height/2); $draw->setFillColor($column_color); $draw->setStrokeColor($column_color); $draw->line($width/2, 0, $width/2, $height); $im->drawImage($draw); $im->setImageFormat('png'); header('Content-type: image/png'); echo $im; $im->destroy();
2. 处理图像
ImageMagick库提供了更多的图像处理方法,同样使用Imagick类的方法,下面是一些常用的图像处理函数:
(1)缩放:$im->scaleImage($dst_width, $dst_height);
(2)旋转:$im->rotateImage($bg_color, $angle);
(3)合并:$im->compositeImage($src_im, Imagick::COMPOSITE_DESTINATIONOVER, $dst_x, $dst_y);
下面是一个使用ImageMagick库处理图像的例子,将一张图片缩小50%并旋转90度:
$src_file = "./test.jpg"; $dst_file = "./test_out.png"; $im = new Imagick(); $im->readImage($src_file); $im->scaleImage($im->getImageWidth()/2, $im->getImageHeight()/2); $bg_color = new ImagickPixel('none'); $rotated_im = $im->rotateImage($bg_color, 90); $rotated_im->writeImage($dst_file); $im->destroy(); $rotated_im->destroy();
总结:
PHP通过GD库和ImageMagick库提供了丰富实用的图像处理方法。使用GD库可以较为方便地生成和处理图像;而ImageMagick库提供了更强大的图像处理方法,同时支持更多的图像格式。根据不同的实际需求,可以选择使用不同的库来进行图像处理。
