HTML Logo by World Wide Web Consortium ( Click to learn more about our commitment to accessibility and standards.

Moving forward with Composr

ocPortal has been relaunched as Composr CMS. ocPortal 9 is superseded by Composr 10.

Head over to for our new site, and to our migration roadmap. Existing ocPortal member accounts have been mirrored.

Alpha blending in PHP using GD: pitfalls to avoid

Alpha blending in PHP using GD: pitfalls to avoid This is a quick blog post for programmers working with PHP alpha blending. There is a lack of clear guidance on this subject and there are a number of stumbling blocks. I'm simply going to list things programmers need to be aware of:
  • GD1 does not support true-color and therefore does not support any alpha blending functions. Be aware of this if you write software you need to run on older web servers.
  • imagefill does not support alpha blending, alpha values are neither blended nor copied. You need to use imagefilledrectangle instead.
  • Always set imagealphablending on the true-color image you are about to manipulate. It sets whether to copy pixels via blending or via copying. Never trust what the default value for it is, explicitly set it.
  • Before saving an image with an alpha channel call imagesavealpha on the true-colour image you are saving.
  • If you are supporting old versions of PHP before 4.3, even if they have GD2 with alpha blending, they don't have the above imagesavealpha function so you can't save alpha blended true-color files – you'll need to save with indexed transparency instead on this PHP environment.
  • Be aware that PHP also supports binary transparency in the same way gif files do. You don't necessarily have to work with true-color, and your image files are not necessarily true-color.
  • It's very confusing but GD stores it's alpha values as 0-127, where 127 is "fully transparent". Not only is the number range halved, it is reversed. Normally we would consider 0 to be transparent, but not in the world of GD!
  • Remember you need to save to a PNG file if you want to preserve your alpha channels. The other image formats supported by GD don't support alpha channels.
  • And of course, remember that IE6 does not support PNG alpha transparency without a code hack.

View all


There have been no trackbacks yet