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

phpserver side programmingprogramming

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
)

相关文章