Skip to content
Related Articles

Related Articles

Improve Article
PHP | Imagick setImageUnits() Function
  • Last Updated : 03 Mar, 2021

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: 
 

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



php




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

Output: 
 

units Before SetUnit = 2
units After Set = 1

Program 2: 
 

php




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

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
Recommended Articles
Page :