Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using imagemagick montage with PHP

I am trying to make a montage using imagemagick. I get it to work partially. I want to make a montage 2 columns by 2 rows. With 5px of padding between images, on a white background. When I use the following code the resulting image is twice as high and twice as wide as one image although only the first of four images appear in its correct size and spot, with white in the remaining three spots. All images are the same dimensions and same filetype.

<?php
header('Content-type: image/jpeg');
$loc1 = 'http://localhost:8888/gallery_edited/0116.jpg';
$loc2 = 'http://localhost:8888/gallery_edited/0115.jpg';
$loc3 = 'http://localhost:8888/gallery_edited/0114.jpg';
$loc4 = 'http://localhost:8888/gallery_edited/0113.jpg';
$image = new Imagick("$loc1 $loc2 $loc3 $loc4");
$image -> setFormat("jpg");
$image = $image -> montageImage(new ImagickDraw, '2x2', '600x400', 0, '0');
echo $image;

enter image description here

like image 890
user1881482 Avatar asked Feb 18 '15 23:02

user1881482


Video Answer


1 Answers

Use the Imagick::addImage to build the image stack with new instances of Imagick object.

<?php

$sources = array(
    'red.png',
    'green.png',
    'blue.png',
    'orange.png'
);

$stack = new Imagick();
foreach( $sources as $source ) {
    $stack->addImage(new Imagick($source));
}

$montage = $stack->montageImage(new ImagickDraw(), '2x2', '500x300', 0, '0');
$montage->writeImage('out.png');

montage with php example

like image 64
emcconville Avatar answered Sep 21 '22 20:09

emcconville