imageresolution 


gd apg

GET or SET the resolution for an image.





This function get the index of an specific pixel color $image.





$image symbolized by identifier returned by one of the image creation functions, such as imagecreate or imagecreatetruecolor.


This function allows to set and get the resolution of an image in DPI, (dots per inch).

If none of the optional parameters is given, the current resolution is returned as indexed array.

If only $resolution_x is given, the horizontal and vertical resolution are set to this value.
If $resolution_x and $resolution_y are given, 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.

This function is available since PHP 7.2.0.

When used as getter, (first signature), it returns an indexed array of the horizontal and vertical resolution on success, or FALSE on failure.

When used as setter, (second signature), this function returns TRUE on success or FALSE on failure.



<?php

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

where,

$image = The image identifier 

$resolution_x 
= The horizontal resolution in DPI

$resolution_y 
= The vertical resolution in DPI

?>
 

  $image   


The image identifier.



  $resolution_x   


The horizontal resolution in DPI, ( dots per inch ).



  $resolution_y   


The vertical resolution in DPI, ( dots per inch ).



  1 EXERCISE   

<?php

echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';

$img_name01 = 'png/PNG 027 03.png';

echo 
$img_name01 . '<br><br><img src="' . 
  
$img_name01 . '" alt="' . $img_name01 . '"><br><br>';

$im01 = imagecreatefrompng($img_name01);

imageresolution($im01);

print_r(imageresolution($im01));

?> 

 RESULT   

PNG 027 03.png

png/PNG 027 03.png apr

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

  2 EXERCISE   

<?php

echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';

$img_name02 = 'gif/GIF 013 02.gif';

echo 
$img_name02 . '<br><br><img src="' . 
 
$img_name02 . '" alt="' . $img_name02 . '"><br><br>';

$im02 = imagecreatefromgif($img_name02);

imageresolution($im02);

print_r(imageresolution($im02));

?>  

 RESULT   

GIF 013 02.gif

GIF 013 02.gif apr

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

  3 EXERCISE   

<?php

// RUN this code several times

echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';

$img_name03 = 'png/PNG 029 03.png'; 

$x03 = 300;

$y03 = 60;

$im03 = imagecreatetruecolor($x03, $y03);

$res_x03 = mt_rand(24,192);

$res_y03 = mt_rand(24,192);

$res_img03 = imageresolution($im03, $res_x03, $res_y03);

$bool03 = imagepng($im03, $img_name03); 

if (
$bool03 == true)   
{   
echo 
$img_name03 . '<br><br>The image has been created!<br><br>'; 

echo 
'<img src="' . $img_name03 . 
'" alt="' . $img_name03 . '" title="' . $img_name03 . '"><br><br>'; 

print_r(imageresolution($im03));  
}   
else   
{   
echo 
'<br><br>The image was NOT created!<br><br>';   
}    

?> 

 RESULT   

PNG 029 03.png

The image has been created!

PNG 029 03.png apr

Array ( [0] => 83 [1] => 175 )

This is just a particular result.