Peter VARGA
Peter VARGA

Reputation: 5186

Set background colour in PHP Imagick montage function

I am composing an image from 6 images using this code:

    $imagick = new Imagick();
    foreach ( $productImages as $productImage ) {
        $imagick->addImage(new Imagick($productImage));
    }

    $categoryCollage = $imagick->montageImage(
        $categoryImage,
        "3x2+0+0",
        "150x100+2+2",
        Imagick::MONTAGEMODE_CONCATENATE,
        "1x1+2+2"
    );

Below the result when images with different size and ratio are added. The background colour is grey. How to set it to white?

According to the manual with the command line version it would be the -background parameter but I don't know how to set it in PHP: https://legacy.imagemagick.org/Usage/montage/

enter image description here

Upvotes: 1

Views: 169

Answers (1)

Peter VARGA
Peter VARGA

Reputation: 5186

I found a very trivial solution where the grey background disappears:

    foreach ( $productImages as $productImage ) {

        $subImage = new Imagick($productImage);
        $subImage->resizeImage(150, 100, Imagick::FILTER_BOX, 1);
        $imagick->addImage($subImage);

    }

I just resize the image to the size which will be then anyway used in the collage and as each original image is bigger, there is no an up-scaling issue.

Using then the Imagick::MONTAGEMODE_FRAME constant it looks then quite OK.

enter image description here

Upvotes: 1

Related Questions