欢迎访问宙启技术站
智能推送

PHP图像处理函数大全,让你轻松操控图片

发布时间:2023-06-13 17:07:48

作为Web应用程序中常见的处理方式之一,处理图像无疑是一个重要的功能。无论是显示用户上传的图像、生成海报、生成二维码还是处理视频缩略图等都需要用到图像处理。本文介绍一些PHP中常用的图像处理函数和操作方法,让你轻松操控图片。

1. 获取图像信息

当我们需要获取图像信息的时候,可以使用PHP的“getimagesize”函数。该函数可以返回图像的多种信息,如文件大小、图像类型等等。

例如:获取图片大小

<?php
$size = getimagesize('test.jpg');
echo '该图像大小为 ' . $size[0] . ' * ' . $size[1];
?>

2. 创建一个新图像

当需要创建一个新的图像时,我们可以使用“imagecreatetruecolor”函数。该函数创建一个指定大小的空白图像。

例如:创建一个400*400的红色图像

<?php
$image = imagecreatetruecolor(400,400);
$red = imagecolorallocate($image,255,0,0);
imagefill($image,0,0,$red);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>

3. 复制图像

如果需要在同一张图片或不同图片中复制某一部分图像时,可以使用PHP图像处理库的“imagecopyresampled”函数。

例如:将原图像的坐标点(150,150)的宽高各100像素的区域复制到新图像的(0,0)位置。

<?php
$src = imagecreatefrompng('origin.png');
list($width,$height) = getimagesize('origin.png');
$newwidth = $width / 2;
$newheight = $height / 2;
$dst = imagecreatetruecolor($newwidth,$newheight);
$image_copy_resampled = imagecopyresampled($dst,$src,0,0,150,150,$newwidth,$newheight,100,100);
header('Content-type: image/png');
imagepng($dst);
imagedestroy($dst);
imagedestroy($src);
?>

4. 调整图像尺寸

将图像的尺寸调整至适当的大小无疑对于提升用户体验有着重要作用。PHP中可用的更改图像尺寸的方法很多,以下是一些例子。

使用“imagecopyresampled”函数可以实现调整图像尺寸的效果。

例如:将原图像放大50%

<?php
$src = imagecreatefrompng('test.png');
list($width,$height) = getimagesize('test.png');
$newwidth = $width * 1.5;
$newheight = $height * 1.5;
$dst = imagecreatetruecolor($newwidth,$newheight);
$image_copy_resampled = imagecopyresampled($dst,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
header('Content-type: image/png');
imagepng($dst);
imagedestroy($dst);
imagedestroy($src);
?>

使用“imagescale”函数可以实现图像缩放效果。

例如:将原图像缩小50%

<?php
$src = imagecreatefrompng('test.png');
list($width,$height) = getimagesize('test.png');
$newwidth = $width / 2;
$newheight = $height / 2;
$dst = imagescale($src,$newwidth,$newheight);
header('Content-type: image/png');
imagepng($dst);
imagedestroy($dst);
imagedestroy($src);
?>

使用“imagecrop”函数可以实现裁剪效果。

例如:将原图像的坐标点(200,200)的宽高各100像素的区域裁剪出来。

<?php
$src = imagecreatefrompng('test.png');
$imageCrop = imagecrop($src, ['x' => 200, 'y' => 200, 'width' => 100, 'height' => 100]);
header('Content-type: image/png');
imagepng($imageCrop);
imagedestroy($imageCrop);
imagedestroy($src);
?>

使用“imagecopymerge”函数可以实现图片合成效果。

例如:将图片B复制合成到图片A上。

<?php
$src1 = imagecreatefrompng('test1.png');
$src2 = imagecreatefrompng('test2.png');
$x = imagesx($src1) - imagesx($src2);
$y = imagesy($src1) - imagesy($src2);
imagecopymerge($src1, $src2, $x, $y, 0, 0, imagesx($src2), imagesy($src2), 100);
header('Content-type: image/png');
imagepng($src1);
imagedestroy($src1);
imagedestroy($src2);
?>

5. 输出图像

当需要输出图像时,我们可以使用“imagepng”或“imagejpeg”等函数。对于输出到浏览器,我们可以使用“header”函数,设置自定义的“Content-type”来输出图像。

例如:输出png类型的图片

<?php
$image = imagecreatefrompng('test.png');
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>

以上是一些常用的PHP图像处理函数和操作方法,将它们灵活应用在实际的项目中,可以大大提升图像处理的效率和精度,让你轻松操控图片。