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