PNG to JPEG, transparent background converts to black

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
pezz

PNG to JPEG, transparent background converts to black

Post by pezz »

When saving a PNG as JPEG by using $im->setimageformat('jpeg'), the resulting saved image has the transparent background replaced with a black background.

Is there any way to change the colour of this background?

Tried a lot of functions to no avail, setimagebackground(), setimagemattecolor().. Hope someone can help me :)
pezz

Re: PNG to JPEG, transparent background converts to black

Post by pezz »

Nevermind, got it.

Code: Select all

$im->PaintOpaqueImage ('black', 'white', 50);
pezz

Re: PNG to JPEG, transparent background converts to black

Post by pezz »

Another method was to put the PNG image data on top of a new, blank, white $im object:

Code: Select all

$im2 = new imagick();
$im2->newImage($im->getImageWidth(),$im->getImageheight(),'white','jpeg');
$im2->compositeimage($im,imagick::COMPOSITE_DEFAULT,0,0);
Post Reply