PHP Imagick cannot set image background color

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
JohnSmith
Posts: 11
Joined: 2012-10-01T16:41:10-07:00
Authentication code: 67789

PHP Imagick cannot set image background color

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

User avatar
fmw42
Posts: 25924
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PHP Imagick cannot set image background color

Post 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?

JohnSmith
Posts: 11
Joined: 2012-10-01T16:41:10-07:00
Authentication code: 67789

Re: PHP Imagick cannot set image background color

Post 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).

User avatar
fmw42
Posts: 25924
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PHP Imagick cannot set image background color

Post 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.

mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: PHP Imagick cannot set image background color

Post 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.
Mikko Koppanen
My blog: http://valokuva.org

DJ Mike
Posts: 33
Joined: 2010-06-29T19:07:53-07:00
Authentication code: 8675308

Re: PHP Imagick cannot set image background color

Post by DJ Mike »

Here is an example changing the bgcolor of a jpeg

http://eclecticdjs.com/mike/tutorials/p ... dcolor.php
DJ Mike's Tutorials: PHP
ImageMagick Functions
http://eclecticdjs.com/mike/tutorials/p ... /index.php

Post Reply