PHP如何使用Gmagick addImage()函数?示例

2021年3月25日12:50:27 发表评论 672 次浏览

Gmagick :: addImage()函数是PHP中的内置函数, 用于向Gmagick对象图像列表添加新图像。此功能从源对象的当前位置向Gmagick对象添加新图像。 Gmagick类具有同时保存和处理多个图像的能力。

语法如下:

bool Gmagick::addImage( $source )

参数:该函数接受单个参数$来源它保存源Gmagick对象。

返回值:成功时, 此函数返回Gmagick对象。

错误/异常:该函数在错误时引发GmagickException。

下面的程序说明了Gmagick :: addImage()PHP中的功能:

原始图片1:

自适应阈值图像

程序:

<?php 
  
// require_once('path/to/vendor/autoload.php');
  
header( 'Content-type: image/png' );
  
$image = new Gmagick (
'https://media.lsbin.org/wp-content/uploads/lsbin-9.png' );
  
$t = new Gmagick (
'https://media.lsbin.org/wp-content/uploads/adaptiveThresholdImage.png' );
  
$image ->addImage( $t );
  
echo $image ;
?>

输出如下:

自适应阈值图像

原始图片2:

原始图像

程式2:

<?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 ); 
$im ->setImageFormat( 'jpeg' ); 
   
$t = new Gmagick (
'https://media.lsbin.org/wp-content/uploads/lsbin-9.png' );
   
$im ->addImage( $t );
    
    
header( "Content-Type: image/jpg" ); 
echo $im ->getImageBlob(); 
?>

输出如下:

PHP | Gmagick addImage()函数1

参考: http://php.net/manual/en/gmagick.addimage.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: