How do you append images that are already created with appendImage?

你离开我真会死。 提交于 2021-01-28 06:32:33

问题


The php website lists the following example:

 <?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;
?>

How do I use an image generated from a URL instead, such as

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

so that I could append the loaded images instead of using newImage()


回答1:


Use Imagick::addImage to "combine" the various Imagicks into one and use appendImages afterwards, for example (addapted from 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;
?>



回答2:


You can use the Handle returned by fopen() to use image from url.

example :

<?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();

?>


来源:https://stackoverflow.com/questions/5280813/how-do-you-append-images-that-are-already-created-with-appendimage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!