2011-03-12 30 views
1

PHP网站列出了以下example如何追加已经用appendImage创建的图像?

<?php 

/* Create new imagick object */ 
$im = new Imagick(); 

/* create red, green and blue images */ 
$im->newImage(100, 50, "red"); 
$im->newImage(100, 50, "green"); 
$im->newImage(100, 50, "blue"); 

/* Append the images into one */ 
$im->resetIterator(); 
$combined = $im->appendImages(true); 

/* Output the image */ 
$combined->setImageFormat("png"); 
header("Content-Type: image/png"); 
echo $combined; 
?> 

我如何使用从一个网址,而不是生成的图像,如

$image = new Imagick("sampleImage.jpg"); 

,这样我可以追加,而不是使用newImage()加载的图片

回答

5

使用Imagick::addImage可将各种Imagicks“组合”为一个,然后使用appendImages,例如(从here加上):

<?php 
$filelist = array("fileitem1.png","fileitem2.png","fileitem3.png"); 

$all = new Imagick(); 

foreach($filelist as $file){ 
    $im = new Imagick($file);  
    $all->addImage($im); 
} 
/* Append the images into one */ 
$all->resetIterator(); 
$combined = $all->appendImages(true); 

/* Output the image */ 
$combined->setImageFormat("png"); 
header("Content-Type: image/png"); 
echo $combined; 
?> 
0

您可以使用fopen()返回的Handle从url中使用图像。

例如:

<?php 
/* Read images from URL */ 
$handle1 = fopen('http://yoursite.com/your-image1.jpg', 'rb'); 
$handle2 = fopen('http://yoursite.com/your-image2.jpg', 'rb'); 

/* Create new imagick object */ 
$img = new Imagick(); 

/* Add to Imagick object */ 
$img->readImageFile($handle1); 
$img->readImageFile($handle2); 

/* Append the images into one */ 
$img->resetIterator(); 
$combined = $img->appendImages(true); 

/* path to save you image */ 
$path = "/images/combined-image.jpg"; 

/* Output the image */ 
$combined->setImageFormat("jpg"); 
$combined->writeimage($path); 

/* destroy imagick objects */ 
$img->destroy(); 
$combined->destroy(); 

?>