How to get or set the resolution of an image using imageresolution() function in PHP?

PHPServer Side ProgrammingProgramming

imageresoulution() is an inbuilt function in PHP that is used to get or set the resolution of an image in dots per inch. If no optional parameters are given, then the current resolution is returned as an indexed array. If one of the optional parameters is given, then it will set both the width and height to that parameter.

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

Syntax

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

Parameters

imageresolution() accepts three parameters: $image, $res_x, $res_y.

  • $image − Specifies the image resource to work on.

  • $res_x − Specifies the horizontal resolution in dots per inch (DPI).

  • $res_y − Specifies the vertical resolution in dots per inch (DPI).

Return Values

imageresolution() returns the indexed array of the image.

Example 1

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

Output

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

Example 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>");
?>

Output

Array
(
   [0] => 300
   [1] => 100
)
raja
Published on 09-Aug-2021 12:53:47
Advertisements