HomeImagickImagickDrawImagickPixelImagick Pixel IteratorImagick KernelDevelopmentTutorial Source codeReport an issue
Category
Example

Append images

← annotateImage   averageImages →
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.

Example

function appendImages()
{
    $images = [
        [
            "../public/images/lories/IMG_1599_480.jpg",
            "../public/images/lories/IMG_2561_480.jpg"
        ],
        [
            "../public/images/lories/IMG_2837_480.jpg",
            "../public/images/lories/IMG_4023_480.jpg"
        ]
    ];
    
    $canvas = new Imagick();

    foreach ($images as $imageRow) {
        $rowImagick = new Imagick();
        $rowImagick->setBackgroundColor('gray');
        foreach ($imageRow as $image_path) {
            $imagick = new Imagick(realpath($image_path));
            $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/jpeg");
    echo $finalimage->getImageBlob();
}