PHP | Imagick setImageUnits() Function

The Imagick::setImageUnits() function is an inbuilt function in PHP which is used to set the units of resolution of a particular image.

Syntax:

bool Imagick::setImageUnits( $units )

Parameters: This function accepts single parameter $units which is used to specify the units to be set for image.

Resolution constants: The Imagick::setImageUnits() function set the Image Units, as described below:

  • imagick::RESOLUTION_UNDEFINED(integer), unit = 0
  • imagick::RESOLUTION_PIXELSPERINCH(integer), unit = 1
  • imagick::RESOLUTION_PIXELSPERCENTIMETER(integer), unit = 2

Return Value: This function returns True on success.



Original Image:
Image

Below programs illustrate the Imagick::setImageUnits() function in PHP:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to illustrate 
// seImageUnits function
$image = new Imagick(
  
// Use getImageUnits function
$units = $image->getImageUnits();
echo "units Before SetUnit = ";
print_r($units);
  
// set iamge unit = 1
$image->setImageUnits(1);
  
// Display the output
$units = $image->getImageUnits();
echo "</br>units After Set = ";
print_r($units);
  
?>

chevron_right


Output:

units Before SetUnit = 2
units After Set = 1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$i = new Imagick(
  
$r = $i->getImageResolution();
$u = $i->getImageUnits();
echo "print previous resolution = ";
print_r($r);
  
// print units
echo "</br>Check units  = ";
print_r($u);
  
// for units are based on below resolution
//0=undefined, 1=pixelsperInch, 2=PixelsPerCentimeter
  
// checking if units = 2
// then set unit = 1
if ($u == Imagick::RESOLUTION_PIXELSPERCENTIMETER) {
   $r[x] = (int)round($r[x] * 2);
   $r[y] = (int)round($r[y] * 2);
   $i->setImageUnits(Imagick::RESOLUTION_PIXELSPERINCH);
   $i->setImageResolution($r[x], $r[y]);
  
  //note that the number type is double again
   $r = $i->getImageResolution();
}
  
// print resolution after
echo "</br>resolution after ";
print_r($r);
  
$u = $i->getImageUnits();
  
echo "</br>units After Set  = ";
print_r($u);
?>

chevron_right


Output:

print previous resolution = Array ( [x] => 37.8 [y] => 37.8 )
Check units = 2
resolution after Array ( [x] => 76 [y] => 76 )
units After Set = 1

Reference: http://php.net/manual/en/imagick.setimageunits.php




My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.