如何在PHP中使用imageresolution()函式獲取或設定影像解析度?


imageresoulution() 是PHP中的一個內建函式,用於獲取或設定影像的解析度(單位為每英寸點數,DPI)。如果未提供可選引數,則返回當前解析度的索引陣列。如果提供了一個或兩個可選引數,則會將寬度和高度都設定為該引數的值。

解析度僅在從支援此類資訊的格式(當前為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
   // Load the png image using imagecreatefrompng() function
   $img = imagecreatefrompng('C:\xampp\htdocs\Images\img34.png');
   
   // Set the image resolution
   imageresolution($img, 300, 100);
   
   // Get the image resolution
   $imageresolution = imageresolution($img);
   print("<pre>".print_r($imageresolution, true)."</pre>");
?>

輸出

Array
(
   [0] => 300
   [1] => 100
)

更新於:2021年8月9日

1K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.