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

PHP图像处理函数列表及用法

发布时间:2023-06-17 18:49:09

PHP是一种广泛使用的服务器端语言,它提供了许多强大的图像处理函数,使开发人员能够轻松地在网站中使用图像。这些函数包括但不限于:创建图像、调整图像大小、旋转图像、裁剪图像、添加水印、生成缩略图等。

以下是常用的PHP图像处理函数及其用法:

1. imagecreatefrompng():创建一个通过PNG图像文件或URL来表示图像的函数。

语法:

resource imagecreatefrompng ( string $filename )

示例:

$img = imagecreatefrompng('image.png');

2. imagecreatefromjpeg():创建一个通过JPEG图像文件或URL来表示图像的函数。

语法:

resource imagecreatefromjpeg ( string $filename )

示例:

$img = imagecreatefromjpeg('image.jpg');

3. imagecreatefromgif():创建一个通过GIF图像文件或URL来表示图像的函数。

语法:

resource imagecreatefromgif ( string $filename )

示例:

$img = imagecreatefromgif('image.gif');

4. imagecreatetruecolor():创建一个真彩色图像。

语法:

resource imagecreatetruecolor ( int $width , int $height )

示例:

$img = imagecreatetruecolor(500, 500);

5. imagesavealpha():设置图像保存完整的alpha通道信息(即透明度)。

语法:

bool imagesavealpha ( resource $image , bool $saveflag )

示例:

imagesavealpha($img, true);

6. imagepng():以PNG格式将图像输出到浏览器或文件。

语法:

bool imagepng ( resource $image [, string $filename [, int $quality]] )

示例:

imagepng($img, 'output.png');

7. imagejpeg():以JPEG格式将图像输出到浏览器或文件。

语法:

bool imagejpeg ( resource $image [, string $filename [, int $quality]] )

示例:

imagejpeg($img, 'output.jpg');

8. imagegif():以GIF格式将图像输出到浏览器或文件。

语法:

bool imagegif ( resource $image [, string $filename] )

示例:

imagegif($img, 'output.gif');

9. imagesx():返回图像宽度像素数。

语法:

int imagesx ( resource $image )

示例:

echo imagesx($img);

10. imagesy():返回图像高度像素数。

语法:

int imagesy ( resource $image )

示例:

echo imagesy($img);

11. imagecopyresampled():复制并重新采样图像的一部分,并调整到指定的大小。

语法:

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 )

示例:

imagecopyresampled($new_img, $img, 0, 0, 0, 0, 100, 100, imagesx($img), imagesy($img));

12. imagerotate():将图像旋转一定角度。

语法:

resource imagerotate ( resource $image , float $angle , int $bgd_color , int $ignore_transparent )

示例:

$rotated = imagerotate($img, 45, 0);

13. imagecrop():裁剪图像。

语法:

resource imagecrop ( resource $image , array $rect )

示例:

$cropped = imagecrop($img, ['x'=>10, 'y'=>10, 'width'=>100, 'height'=>100]);

14. imagecopy():将一图像合并到另一图像上。

语法:

bool imagecopy ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )

示例:

imagecopy($new_img, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));

15. imagestring():用指定的字体和字号在图像上写一行文字。

语法:

bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )

示例:

imagestring($img, 5, 20, 20, 'Hello World!', 0xffffff);

16. imagefttext():用指定的字体和字号在图像上写一行TrueType文字。

语法:

array imagefttext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text [, array $extrainfo ] )

示例:

imagefttext($img, 16, 0, 20, 20, 0xffffff, 'arial.ttf', 'Hello World!');

17. imagecopymerge():以一定的透明度将一图像合并到另一图像上。

语法:

bool imagecopymerge ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )

示例:

imagecopymerge($new_img, $img, 0, 0, 0, 0, imagesx($img), imagesy($img), 50);

18. imagecopymergegray():以一定的透明度将一灰度图像合并到另一图像上。

语法:

bool imagecopymergegray ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )

示例:

imagecopymergegray($new_img, $gray_image, 0, 0, 0, 0, imagesx($gray_image), imagesy($gray_image), 50);

19. imagefilter():对图像应用一个或多个过滤器。

语法:

bool imagefilter ( resource $image , int $filtertype [, int $arg1 [, int $arg2 [, int $arg3 [, int $arg4 ]]]] )

示例:

imagefilter($img, IMG_FILTER_GRAYSCALE);

20. imagecopyresized():以指定的尺寸复制并缩放图像的一部分。

语法:

bool imagecopyresized ( 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 )

示例:

imagecopyresized($new_img, $img, 0, 0, 0, 0, 100, 100, imagesx($img), imagesy($img));

21. imageinterlace():调整图像的交错选项。

语法:

int imageinterlace ( resource $image , int $interlace = 0 )

示例:

imageinterlace($img, 1);

22. imageflip():翻转图像。

语法:

bool imageflip ( resource $image , int $mode )

示例:

imageflip($img, IMG_FLIP_HORIZONTAL);

23. imagescale():缩放图像到指定大小。

语法:

resource imagescale ( resource $image , int $new_width [, int $new_height = -1 [, int $mode = IMG_BILINEAR_FIXED ]] )

示例:

$scaled = imagescale($img, 100, 100);

24. imagealphablending():设置alpha混合标志。

语法:

bool imagealphablending ( resource $image , bool $blendmode )

示例:

imagealphablending($img, true);

25. imagelayereffect():