Forum Moderators: coopster

Message Too Old, No Replies

How to place white background behind a JPEG converted from PNG?

         

irock

10:31 pm on Sep 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have been using this program to convert non-transparent PNG to JPEG. But now, I need to convert transparent PNG to JPEG, and the resulting JPEG has a BLACK background while I really need a WHITE or GRAY background. Could anyone help please?

<?php
Header("Content-type: image/jpeg");
$filenamee = "$DOCUMENT_ROOT/images/list/$filename";
$orig_image = imagecreatefrompng($filenamee);
list($width, $height, $type, $attr) = getimagesize($filenamee);
if ($width > $size) {
$ratio = $size / $width;
$newwidth = $size;
$newheight = $ratio * $height; }
if ($width <= $size) {
$newwidth = $width;
$newheight = $height; }
$sm_image = imagecreatetruecolor($newwidth,$newheight) or die ("Cannot Initialize new GD image stream");;
Imagecopyresampled($sm_image,$orig_image,0,0,0,0,$newwidth,$newheight,imagesx($orig_image),imagesy($orig_image));
imageJPEG($sm_image);
imagedestroy($sm_image);
imageDestroy($orig_image);
?>

mincklerstraat

9:48 am on Sep 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's been a while since I've been monkeying with gd, but my heart goes out to you - transparency in gd can be a pain, and it can also show different behaviors depending on your php / gd install. Very frustrating. What works on my local debian box doesn't work on my hosted red hat server.

Your question *might* have an easy answer:
you tried an imagefilledrectangle(<correct params go here>) on your $sm_image after having done an imagecolorallocate() for this image, creating a grey or white color? If not, of course it'll have a black background, since images when first created are black.

If this doesn't work, look over the various switch functions very carefully in gd, such as imagealphablending() and imagesavealpha().

irock

7:44 am on Sep 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks. I will investigate those functions.