Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Imagick cannot set image background color

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");
like image 212
John Smith Avatar asked Aug 05 '26 14:08

John Smith


1 Answers

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);
like image 55
Simon Epskamp Avatar answered Aug 08 '26 10:08

Simon Epskamp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!