PHP 8.3.4 Released!

imageresolution

(PHP 7 >= 7.2.0, PHP 8)

imageresolution画像の解像度を取得/設定する

説明

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

imageresolution() 関数は、 画像の解像度を DPI(dots per inch) 単位で取得/設定します。 オプションのパラメータが null だった場合は、 現在の解像度を配列で返します。 resolution_x だけが null でない場合、 垂直/水平方向の解像度がこの値に設定されます。 オプションのパラメータが両方指定されると、 垂直/水平方向の解像度がそれぞれ指定された値に設定されます。

この手の情報をサポートしている画像フォーマットを読み書きする場合(現状、PNG と JPEG)に、 解像度情報はメタ情報としてのみ使われます。画像を描画する操作には一切影響しません。 新しい画像の解像度は 96DPI です。

パラメータ

image

imagecreatetruecolor()のような画像作成関数が返す GdImage オブジェクト。

resolution_x

水平方向の解像度をDPI単位で指定します。

resolution_y

垂直方向の解像度をDPI単位で指定します。

戻り値

getter として使う場合、 水平方向と垂直方向の解像度を成功時には返します。 失敗した場合に false を返します。 setter として使う場合、 成功時には true を返し、失敗した場合に false を返します。

変更履歴

バージョン 説明
8.0.0 resolution_xresolution_y は、nullable になりました。

例1 画像の解像度を取得/設定する

<?php
$im
= imagecreatetruecolor(100, 100);
imageresolution($im, 200);
print_r(imageresolution($im));
imageresolution($im, 300, 72);
print_r(imageresolution($im));
?>

上の例の出力は以下となります。

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

User Contributed Notes 1 note

up
1
fernando dot fonseca at afteryou dot pt
2 years ago
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);
To Top