Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Images Wish List | Report Bug | About Me ]

11.2.9     Adding transparency: imagecolortransparent()

This is NOT the latest copy of this book; click here for the latest version.

int imagecolortransparent ( resource image, int color)

Specifying that part of an image should be transparent is as simple as picking the colour to use as transparent and passing it into this function. As the support for transparency in some browsers (notably Internet Explorer) is limited, this function is most useful when the transparent image is used as part of a larger image so that the transparency can be seen.

<?php
  $image
= imagecreatetruecolor(400,400);

  
$black = imagecolorallocate($image, 0, 0, 0);
  
imagecolortransparent($image, $black);

  
/// rest of picture here
?>

Author's Note: JPEGs do not support transparency and will likely never do so. The reason for this is because both methods of transparency - colour selection and alpha channels - are unsuitable for the JPEG format. The first is because JPEGs do not guarantee exact colour matching, which means that a colour you expect to be transparent may end up not. The second is because alpha channels usually have large blocks of transparency followed by a quick change to non-transparency - something that JPEG handles very badly because it relies on smooth changes in colours to compress well.





<< 11.2.8 Colour and image fills: imagefill(), imagefilltoborder(), and imagesettile()   11.2.10 Using brushes: imagesetbrush() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
joe@zerovektor.com - 05 Dec 2008

If you use the advice from this page http://www.hudzilla.org/phpbook/read.php/11_2_29 and limit the colors to 256 or less, then the transparency will work in Internet Explorer.

imagetruecolortopalette($image, true, 256);

I do that right before sending the header and image.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow