php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66348 setBackgroundColor() has no effect
Submitted: 2013-12-25 08:50 UTC Modified: 2013-12-25 09:57 UTC
From: f21 dot groups at gmail dot com Assigned:
Status: Not a bug Package: imagick (PECL)
PHP Version: 5.5.7 OS: Ubuntu 13.10 64-bit
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: f21 dot groups at gmail dot com
New email:
PHP Version: OS:

 

 [2013-12-25 08:50 UTC] f21 dot groups at gmail dot com
Description:
------------
setBackgroundColor() does not have any effect.
Imagemagick is compiled from source and the version is ImageMagick 6.8.7-10 Q8 x86_64.

Test script:
---------------
$canvas = new \Imagick();
$canvas->newImage( 50, 50, 'blue');
$canvas->setBackgroundColor('red');
$canvas->writeImage('test.png');

Expected result:
----------------
The image should be red.

Actual result:
--------------
Created image is blue.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-25 09:15 UTC] mkoppanen@php.net
-Status: Open +Status: Not a bug
 [2013-12-25 09:15 UTC] mkoppanen@php.net
I am not sure if I agree with the expected result. When you call newImage you create a new image where every pixel is blue.

setBackgroundColor is used for example when reading in formats that don't have background color. Also not that "set<something>" affects all subsequent operations where as "setImage<something>" affects the current image pointer.
 [2013-12-25 09:41 UTC] f21 dot groups at gmail dot com
Good point!

The problem is that I am trying to work around a bug in imagemagick's coalesce for resizing GIF animations where coalesce will introduce some random background colors.

The workaround is to set -background none when coalescing:
convert animation.gif -background none -coalesce temporary.gif

The problem is that there seems to be no way to set the background to none when running coalesce:

$original = new \Imagick();
$original->readImage($image);
$original = $original->coalesceImages();
$original->setBackgroundColor('none'); //Did not work
$original->setImageBackgroundColor('none'); //Did not work either.
 [2013-12-25 09:43 UTC] mkoppanen@php.net
As mentioned, setBackgroundColor will affect _subsequent_ operations. Hence the code should probably be:

$original = new \Imagick();
$original->setBackgroundColor('none');
$original->readImage($image);
$original = $original->coalesceImages();
 [2013-12-25 09:49 UTC] f21 dot groups at gmail dot com
That was also something I tried.

Unfortunately, the setBackgroundColor() did not seem to have any effect.

Here is the gif if you are interested: http://i.stack.imgur.com/6m3RI.gif
 [2013-12-25 09:57 UTC] mkoppanen@php.net
Does the following solve the issue:


$original = new \Imagick();
$original->readImage('test.gif');

foreach ($original as $frame) {
    $frame->thumbnailImage (160, null);
    $frame->setImageBackgroundColor('none');
}

$original = $original->coalesceImages();
$original->writeImages ('test2.gif', true);
 [2013-12-25 10:03 UTC] f21 dot groups at gmail dot com
Perfect! That appears to have solved the problem!

Would love to see some more documentation for setImageBackgroundColor() and setBackgroundColor() as they are a bit sparse at the moment.

Would love to have a way to do this without having to loop through all the frames though.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 05 14:01:34 2025 UTC