imageresolution

(PHP 7 >= 7.2.0, PHP 8)

imageresolutionGet or set the resolution of the image

说明

imageresolution ( GdImage $image , int|null $resolution_x = null , int|null $resolution_y = null ) : array|bool

imageresolution() allows to set and get the resolution of an image in DPI (dots per inch). If the optional parameters are null, the current resolution is returned as an indexed array. If only resolution_x is not null, the horizontal and vertical resolution are set to this value. If none of the optional parameters are null, the horizontal and vertical resolution are set to these values, respectively.

The resolution is only used as meta information when images are read from and written to formats supporting this kind of information (curently PNG and JPEG). It does not affect any drawing operations. The default resolution for new images is 96 DPI.

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的图象资源。

resolution_x

The horizontal resolution in DPI.

resolution_y

The vertical resolution in DPI.

返回值

When used as getter, it returns an indexed array of the horizontal and vertical resolution on success, 或者在失败时返回 false. When used as setter, it returns true on success, 或者在失败时返回 false.

更新日志

版本 说明
8.0.0 resolution_x and resolution_y are now nullable.

范例

Example #1 Setting and getting the resolution of an image

<?php
$im 
imagecreatetruecolor(100100);
imageresolution($im200);
print_r(imageresolution($im));
imageresolution($im30072);
print_r(imageresolution($im));
?>

以上例程会输出:

Array
(
    [0] => 200
    [1] => 200
)
Array
(
    [0] => 300
    [1] => 72
)

User Contributed Notes

fernando dot fonseca at afteryou dot pt 25-Jun-2021 02:59
It should be clear that the set version of the function doesn't change the image it self, just the resource in memory, which probably is fine if you didn't save the image yet. If your use case is for an image that is already on disk,  image you should always do something like:

imageresolution($img, 300, 300);
imagepng($img, $filname, $quality);