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

PHP图像处理函数:10个让你的图片更美观的函数

发布时间:2023-06-23 23:56:28

PHP图像处理函数是用于在PHP中处理图像的一组内建函数。这些函数可以用于实现各种图像操作,例如裁剪、缩放、旋转、改变颜色和亮度等。使用这些函数,我们可以轻松地创建出各种有趣的效果和美观的图像。在本文中,我们将介绍10个让你的图片更美观的PHP图像处理函数。

1. imagecreatetruecolor()

imagecreatetruecolor()函数可以用于创建一幅指定大小的彩色图像。它的输入参数为图像的宽度和高度,返回值为一个新创建的图像资源标识符。例如,下面的代码创建了一个500x500像素的白色背景图像:

$img = imagecreatetruecolor(500, 500);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);

2. imagecopyresampled()

imagecopyresampled()函数可以用于实现图像缩放和裁剪。它将一个源图像复制到一个目标图像中,并对源图像进行缩放和裁剪。它的输入参数包括源图像、目标图像、目标图像的左上角坐标、源图像的左上角坐标、目标图像的宽度和高度、源图像的宽度和高度。例如,下面的代码将一个1000x1000像素的图像缩放为200x200像素:

$src_img = imagecreatefromjpeg('input.jpg');
$dest_img = imagecreatetruecolor(200, 200);

$x_ratio = imagesx($src_img) / 200;
$y_ratio = imagesy($src_img) / 200;

if ($x_ratio > $y_ratio) {
  $src_w = round(imagesx($src_img) / $x_ratio);
  $src_h = round(imagesy($src_img) / $x_ratio);
  $src_x = 0;
  $src_y = round((imagesy($src_img) - $src_h) / 2);
} else {
  $src_w = round(imagesx($src_img) / $y_ratio);
  $src_h = round(imagesy($src_img) / $y_ratio);
  $src_x = round((imagesx($src_img) - $src_w) / 2);
  $src_y = 0;
}

imagecopyresampled($dest_img, $src_img, 0, 0, $src_x, $src_y, 200, 200, $src_w, $src_h);

imagejpeg($dest_img, 'output.jpg', 100);

3. imagefilter()

imagefilter()函数可以用于实现图像的滤镜效果。它的输入参数包括图像资源、滤镜类型和参数等。例如,下面的代码使用模糊滤镜生成一个模糊的图像:

$img = imagecreatefromjpeg('input.jpg');
imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);
imagejpeg($img, 'output.jpg', 100);

4. imagettftext()

imagettftext()函数可以用于实现图像中的文字效果。它的输入参数包括图像资源、字体大小、字体角度、文字坐标、字体颜色、字体文件等。例如,下面的代码在图像中写入一段文字:

$img = imagecreatefromjpeg('input.jpg');
$white = imagecolorallocate($img, 255, 255, 255);
imagettftext($img, 20, 0, 10, 30, $white, 'font.ttf', 'Hello World!');
imagejpeg($img, 'output.jpg', 100);

5. imagecolorallocate()

imagecolorallocate()函数可以用于创建图像的颜色。它的输入参数包括图像资源、颜色的红、绿、蓝三原色值等。例如,下面的代码创建了一个红色的颜色:

$img = imagecreatetruecolor(500, 500);
$red = imagecolorallocate($img, 255, 0, 0);

6. imagecrop()

imagecrop()函数可以用于实现图像的裁剪。它的输入参数包括图像资源、裁剪的矩形框的左上角坐标和宽度、高度等。例如,下面的代码将一个图像裁剪为200x200像素的正方形:

$img = imagecreatefromjpeg('input.jpg');
$cropped_img = imagecrop($img, ['x' => 0, 'y' => 0, 'width' => 200, 'height' => 200]);
imagejpeg($cropped_img, 'output.jpg', 100);

7. imagecopy()

imagecopy()函数可以用于实现图像的拼接。它将一个源图像复制到一个目标图像中,并指定拼接的位置和大小等。例如,下面的代码将两个200x200的图像拼接为400x200的图像:

$src_img1 = imagecreatefromjpeg('input1.jpg');
$src_img2 = imagecreatefromjpeg('input2.jpg');
$dest_img = imagecreatetruecolor(400, 200);

imagecopy($dest_img, $src_img1, 0, 0, 0, 0, 200, 200);
imagecopy($dest_img, $src_img2, 200, 0, 0, 0, 200, 200);

imagejpeg($dest_img, 'output.jpg', 100);

8. imagecreatetruecolor()

imagecreatetruecolor()函数也可以用于实现图像的背景透明效果。它的输入参数包括图像的宽度和高度,返回值为一个新创建的图像资源标识符。例如,下面的代码创建了一个500x500像素的透明背景图像:

$img = imagecreatetruecolor(500, 500);
imagesavealpha($img, true);
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $transparent);

9. imagecolorize()

imagecolorize()函数可以用于调整图像的颜色饱和度和亮度等。它的输入参数包括图像资源、红、绿、蓝三种颜色的值、透明度等。例如,下面的代码将一个图像调整为蓝色:

$img = imagecreatefromjpeg('input.jpg');
imagefilter($img, IMG_FILTER_COLORIZE, 0, 0, 255, 0);
imagejpeg($img, 'output.jpg', 100);

10. imagegrayscale()

imagegrayscale()函数可以用于将图像转换为灰度图像。它的输入参数为图像资源。例如,下面的代码将一个图像转换为灰度图像:

$img = imagecreatefromjpeg('input.jpg');
imagefilter($img, IMG_FILTER_GRAYSCALE);
imagejpeg($img, 'output.jpg', 100);

总结

PHP图像处理函数可以帮助我们实现各种有趣的效果和美观的图像。本文介绍了10个常用的PHP图像处理函数,包括图像的缩放、裁剪、滤镜、文字、颜色和透明度等操作。使用这些函数,我们可以轻松地创建出各种有趣的图像效果。