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


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


Imagick::setImageResolution()函数是PHP中的内置函数,用于设置图像对象的分辨率。

用法:

bool Imagick::setImageResolution($x_resolution, $y_resolution)

参数:该函数接受上述和以下所述的两个参数:


  • $x_resolution:这是必填参数,用于指定x轴分辨率。
  • $y_resolution:这是必需的参数,用于指定y轴分辨率。

返回值:此函数以数组形式返回分辨率。

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

原始图片:
https://media.geeksforgeeks.org/wp-content/uploads/geeks-21.png

程序1:

<?php 
  
$imagick = new Imagick( 
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-15.png'); 
  
// Getting Resolution of image 
// using getimageresolution function 
$res = $imagick->getImageResolution(); 
echo "X = ".$res['x'] . "</br>"; 
echo "Y = ".$res['y'] . "</br>"; 
  
// Function to set image resolution 
$imagick->setImageResolution(50, 50); 
  
  
echo "After Set Resolution: </br>"; 
  
// Getting Resolution of image 
// using getimageresolution function 
$res = $imagick->getImageResolution(); 
echo "X = " . $res['x'] . "</br>"; 
echo "Y = " . $res['y'] . "</br>"; 
?>

输出:

X = 37.8
Y = 37.8
After Set Resolution:
X = 50
Y = 50

原始图片:https://media.geeksforgeeks.org/wp-content/uploads/Screenshot-from-2018-10-16-23-23-54.png

程序2:

<?php  
$string = "Computer Science portal for Geeks!";  
     
// Creating new image of above String  
// and add color and background  
$im = new Imagick();  
$draw = new ImagickDraw();  
    
// Fill the color in image  
$draw->setFillColor(new ImagickPixel('green'));  
    
// Set the text font size  
$draw->setFontSize(50);  
    
$metrix = $im->queryFontMetrics($draw, $string);  
$draw->annotation(0, 40, $string);  
$im->newImage($metrix['textWidth'], $metrix['textHeight'],  
         new ImagickPixel('white'));  
             
// Draw the image           
$im->drawImage($draw); 
echo "Before: </br>"; 
  
// Getting Resolution of created image 
// using getimageresolution function 
$res = $im->getImageResolution(); 
echo "X = ".$res['x'] . "</br>"; 
echo "Y = ".$res['y'] ." </br>"; 
  
  
// Set image resolution (50, 50)  
$im->setImageResolution(50, 50);  
  
echo "After:</br> "; 
  
// Getting Resolution of created image 
// using getimageresolution function 
$res = $im->getImageResolution(); 
echo "X = ".$res['x'] . "</br>"; 
echo "Y = ".$res['y'] ." </br>"; 
?> 

输出:

Before:
X = 0
Y = 0
After:
X = 50
Y = 50

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



相关用法


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