PHP Imagick cannot set image background color

2019-07-19 01:14发布

问题:

I load transparent png image and i every time i try to set bg color using setimagebackgroundcolor() it still transparent

    $input_img = new Imagick();

    $input_img->setBackgroundColor("#ff0000");

    $input_img->readImage("transparent.png");

    $input_img->setimagebackgroundcolor("#00ff00");

    $input_img->setImageFormat("png");

    $input_img->setimagebackgroundcolor("#ff00ff");

    $input_img->writeimage("image.png");

回答1:

First line of the code sets a new Imagickpixel object for the colors. Second line creates a new frame and 1920 and 1200 is ofcourse the dimensions.

$color = new ImagickPixel("white");

$input_img->newImage(1920,1200, $color)


回答2:

The trick is using: $im = $im->flattenImages();:

<?php
$im = new Imagick($filename);

$im->setImageBackgroundColor('#ffffff');
$im = $im->flattenImages();

$im->setImageFormat("jpeg");
$im->setImageCompressionQuality(95);
$im->writeImage($filename);


回答3:

Try this!

$imput_img->setBackgroundColor(new ImagickPixel('blue'));