Adding transparency

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.

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


If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Using brushes >>

Previous chapter: Colour and image fills

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.