如何使用 PHP 中的 imageresolution() 函数获取或设置图像的分辨率?

imageresoulution()是 PHP 中的一个内置函数,用于获取或设置图像的分辨率(以每英寸点数为单位)。如果没有给出可选参数,则当前分辨率作为索引数组返回。如果给出了可选参数之一,则它将宽度和高度都设置为该参数。

当图像被读取和写入支持此类信息的格式(当前为 PNG 和 JPEG)时,分辨率仅用作元信息。它不影响任何绘图操作。96 DPI(每英寸点数)是新图像的默认分辨率。

语法

mixed imageresolution(resource $image, int $res_x, int $res_y)

参数

imageresolution() 接受三个参数:$image、$res_x、$res_y。

  • $image - 指定要处理的图像资源。

  • $res_x - 以每英寸点数 (DPI)为单位指定水平分辨率。

  • $res_y - 以每英寸点数 (DPI)为单位指定垂直分辨率。

返回值

imageresolution() 返回图像的索引数组。

示例 1

<?php
   $img = imagecreatetruecolor(100, 100);
   imageresolution($img, 200);
   print_r(imageresolution($img));
   imageresolution($img, 300, 72);
   print_r(imageresolution($img));
?>
输出结果
Array
(
   [0] => 200
   [1] => 200
)
Array
(
   [0] => 300
   [1] => 72
)

示例 2

<?php
   // 使用 imagecreatefrompng() 函数加载 png 图像
   $img = imagecreatefrompng('C:\xampp\htdocs\Images\img34.png');
   
   // 设置图像分辨率
   imageresolution($img, 300, 100);
   
   // 获取图像分辨率
   $imageresolution = imageresolution($img);
   print("<pre>".print_r($imageresolution, true)."</pre>");
?>
输出结果
Array
(
   [0] => 300
   [1] => 100
)

猜你喜欢