當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP Gmagick setimageresolution()用法及代碼示例


Gmagick::setimageresolution()函數是PHP中的內置函數,用於設置圖像對象的分辨率。

用法:

Gmagick Gmagick::setImageResolution($x_resolution, $y_resolution)

參數:該函數接受上述和以下所述的兩個參數:


  • $x_resolution:這是必填參數,用於指定x軸分辨率。
  • $y_resolution:這是必需的參數,用於指定y軸分辨率。

返回值:成功時,此函數返回Gmagick對象。

以下示例程序旨在說明PHP中的Gmagick::setimageresolution()函數:

程序1:
原始圖片:
https://media.geeksforgeeks.org/wp-content/uploads/geeks-21.png

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

輸出:

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

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

<?php  
$string = "Computer Science portal for Geeks!";  
     
// Creating new image of above String  
// and add color and background  
$im = new Gmagick();  
$draw = new GmagickDraw();  
    
// Fill the color in image  
$draw->setFillColor(new GmagickPixel('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 GmagickPixel('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/gmagick.setimageresolution.php



相關用法


注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | Gmagick setimageresolution() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。