Open In App

PHP | imageresolution() Function

Last Updated : 06 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The imageresolution() function is an inbuilt function in PHP which is used to set and return the resolution of an image in DPI (dots per inch). If none of the optional parameters is given, the current resolution is returned as an indexed array. If one of the optional parameters is given it will set both width and height to that parameter. The resolution is only used as meta information when images are read from and written to formats supporting this kind of information (currently PNG and JPEG). It does not affect how the image looks. The default resolution for new images is 96 DPI.
Syntax: 

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

Parameters: This function accepts three parameters as mentioned above and described below: 

  • $image: It specifies the image resource to work on.
  • $res_x (Optional): It specifies the horizontal resolution in DPI.
  • $res_y (Optional): It specifies the vertical resolution in DPI.

Return Value: This function returns the indexed array when used as getter and when used as setter returns TRUE on success or FALSE on failure.
Below examples illustrate the imageresolution() function in PHP:
Example 1: In this example we will get the resolution of the image. 

php




<?php
 
// Load the png image
$image = imagecreatefrompng(
 
// Get the image resolution
$imageresolution = imageresolution($image);
print("<pre>".print_r($imageresolution, true)."</pre>");
?>


Output: 

Array
(
    [0] => 96
    [1] => 96
)

Example 2: In this example we will set the resolution of the image, 

php




<?php
 
// Load the png image
$image = imagecreatefrompng(
 
// Set the resolution
imageresolution($image, 400, 200);
 
// Get the image resolution
$imageresolution = imageresolution($image);
print("<pre>".print_r($imageresolution, true)."</pre>");
?>


Output: 

Array
(
    [0] => 400
    [1] => 200
)

Reference: https://www.php.net/manual/en/function.imageresolution.php



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads