Page 1 of 1

PHP Imagick cannot set image background color

Posted: 2012-10-30T18:06:57-07:00
by JohnSmith
I load transparent png image and i every time i try to set bg color using `setimagebackgroundcolor()` it still transparent


Code: Select all

       $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");

Re: PHP Imagick cannot set image background color

Posted: 2012-10-30T18:12:17-07:00
by fmw42
In Imagemagick, there is no such command as -backgroundcolor. And Imagick has no command setBackgroundColor. It is setImageBackgroundColor.

I think the latter is equivalent to the command line -background somecolor?

-background is a setting, not an operation. It needs to be used by another option such as -extent.

You cannot change the background color of an existing image by just using -background color.

Lets say you have an image with a large canvas of red and in the middle you have a small square of blue. Using -background color to change red to green with do nothing. You would need to use the equivalent of -fuzz XX% -fill green -opaque red. See http://www.imagemagick.org/Usage/color_basics/#replace

If all you are trying to do is create an image of some flat color, then you probably need to do that with the creation of the image object. See http://www.php.net/manual/en/imagick.newimage.php


Perhaps I misunderstand what you are trying to do. Can you be a bit more clear about what you are trying to do. Also what version of IM, what platform, what version of Imagick?

Re: PHP Imagick cannot set image background color

Posted: 2012-10-31T09:45:40-07:00
by JohnSmith
Image was transparent, so red convas in middle and around it is transparency. I dont know about how i can use command line arguments here because i am working with PHP extension and i dont want to use shell_exec() etc.

setBackgrounColor() described here http://www.php.net/manual/en/imagick.se ... dcolor.php

its what you do before importing image in php Imagick object and if you want to work with image thats already imported/created inside object you use setImageBackgrounColor() i guess.

My problem transparent image when i assign it to imagick object becomes non transparent (white background appears).

Re: PHP Imagick cannot set image background color

Posted: 2012-10-31T10:48:52-07:00
by fmw42
setBackgrounColor() described here http://www.php.net/manual/en/imagick.se ... dcolor.php

its what you do before importing image in php Imagick object and if you want to work with image thats already imported/created inside object you use setImageBackgrounColor() i guess.
I really doubt that. I think it is used before http://www.php.net/manual/en/imagick.extentimage.php

If you have a transparent image, then you have to specify colors with the alpha values as well in your hex colors. see http://www.imagemagick.org/script/color.php

I was not suggesting you use exec() with command line functions. I was just telling you the equivalent operations in command line so that you can find the appropriate ones in Imagick.

I am not an Imagick expert.

Re: PHP Imagick cannot set image background color

Posted: 2013-01-18T03:04:45-07:00
by mkoppanen
Hello,

imagick::setImageBackgroundColor maps to http://www.imagemagick.org/api/magick-i ... roundColor

imagick::setBackgroundColor maps to http://www.imagemagick.org/api/magick-p ... roundColor

These methods don't apply reading PNGs with transparency if I remember correctly. I would imagine creating a canvas of color and overlaying transparent.png on top of it would produce the results you are after.

Re: PHP Imagick cannot set image background color

Posted: 2013-02-09T09:17:47-07:00
by DJ Mike
Here is an example changing the bgcolor of a jpeg

http://eclecticdjs.com/mike/tutorials/p ... dcolor.php