当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP Imagick setImageUnits()用法及代码示例


Imagick::setImageUnits()函数是PHP中的内置函数,用于设置特定图像的分辨率单位。

用法:

bool Imagick::setImageUnits( $units )

参数:该函数接受单个参数$units,该参数用于指定要为图像设置的单位。


分辨率常数:Imagick::setImageUnits()函数设置图像单位,如下所述:

  • imagick::RESOLUTION_UNDEFINED(整数),单位= 0
  • imagick::RESOLUTION_PIXELSPERINCH(整数),单位= 1
  • imagick::RESOLUTION_PIXELSPERCENTIMETER(整数),单位= 2

返回值:成功时此函数返回True。

原始图片:

以下示例程序旨在说明PHP中的Imagick::setImageUnits()函数:
程序1:

<?php 
  
// PHP program to illustrate  
// seImageUnits function 
$image = new Imagick( 
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-15.png'); 
  
// 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); 
  
?>

输出:

units Before SetUnit = 2
units After Set = 1

程序2:

<?php 
$i = new Imagick( 
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-15.png'); 
  
$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); 
?>

输出:

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

参考:http://php.net/manual/en/imagick.setimageunits.php



相关用法


注:本文由纯净天空筛选整理自R_Raj大神的英文原创作品 PHP | Imagick setImageUnits() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。