Append a set of images into one larger image.
This function only works if the internal iterator in the image is reset to 0 before the function is called.
Append a set of images into one larger image.
This function only works if the internal iterator in the image is reset to 0 before the function is called.
Parameters
stackboolWhether to stack the images vertically. By default (or if False; is specified) images are stacked left-to-right. If stack is True;, images are stacked top-to-bottom.
function appendImages()
{
    $images = [
        [
            "../imagick/images/lories/IMG_1599_480.jpg",
            "../imagick/images/lories/IMG_2561_480.jpg"
        ],
        [
            "../imagick/images/lories/IMG_2837_480.jpg",
            "../imagick/images/lories/IMG_4023_480.jpg"
        ]
    ];
    
    $canvas = new Imagick();

    foreach ($images as $imageRow) {
        $rowImagick = new Imagick();
        $rowImagick->setBackgroundColor('gray');
        foreach ($imageRow as $imagePath) {
            $imagick = new Imagick(realpath($imagePath));
            $imagick->setImageBackgroundColor("gray");
            $imagick->resizeimage(200, 200, \Imagick::FILTER_LANCZOS, 1.0, true);
            $rowImagick->addImage($imagick);
        }
        $rowImagick->resetIterator();
        //Add the images horizontally.
        $combinedRow = $rowImagick->appendImages(false);
        $canvas->addImage($combinedRow);
    }

    $canvas->resetIterator();
    
    //Add the images vertically.
    $finalimage = $canvas->appendImages(true);
    $finalimage->setImageFormat('jpg');

    header("Content-Type: image/jpg");
    echo $finalimage->getImageBlob();
}