Friday, October 24, 2025
HomeLanguagesPHP | Imagick setImageUnits() Function

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: 
 

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
 

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS