image - transparent

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

image - transparent

Post by tecktalkcm0391 »

How do you make PHP make a jpeg image background transparent?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: image - transparent

Post by alex.barylski »

tecktalkcm0391 wrote:How do you make PHP make a jpeg image background transparent?
You can't...JPEG doesn't support transparent backgrounds

You have to use either GIF or PNG or use a mask on your JPEG which is unessecary (and useless in IMG tags) when you can just use PNG...

PNG is the better choice... :)

IE doesn't render PNG properly but I believe it's fixed in IE7...however search google as I believe there is a hack you can use to correct this issue :)

Cheers :)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Yeah, I just thought about that, well how could you do it if the image was a gif or png?
bitbytes
Forum Newbie
Posts: 2
Joined: Mon May 22, 2006 8:14 am

Post by bitbytes »

am looking into it . thanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

It is also possible to make a transparent 8 bit (palette image) .png. This will be one third the filesize and smaller than a comparitive .gif image. Also if the transparency is single channel, full transparency (127), the image should work fine in IE. If you are going to use less than 256 colours go for the 8 bit format.

Code: Select all

<?php

$image = imagecreate(200, 200);
$transparent_red = ImageColorAllocateAlpha($image, 255, 0, 0, 127);
imagefill($image, 0, 0, $transparent_red);
header("Content-type: image/png");
imagepng($image);

?>
Post Reply