Transparency and alpha blending

This tutorial covers some aspects of PHP GD transparency. Besides using imagecolortransparent($image,$color); for the background, you can also allocate colors to have transparency by using imagecolorallocatealpha($image,R,G,B,A); where A is a value from 0-127 with 127 being totally transparent (or invisible).

Right click, select all, ctrl+c to copy.

You can see from these 2 examples that with alpha color the background also shows through, making the gray background colors appear darker.
There is an alpha blending technique (which only works with a gray background). By applying this flag, you get a more blended color (as opposed to a see through color).
imagelayereffect($image, IMG_EFFECT_OVERLAY);
You can turn off the overlay effect by using:
imagelayereffect($image, IMG_EFFECT_REPLACE);
In this sample the green ellipse follows the replace tag.
You can also revert to the see through colors by using:
imagelayereffect($image, IMG_EFFECT_ALPHABLEND);
or
imagelayereffect($image, IMG_EFFECT_NORMAL);

Before the green ellipse. They work the same.


From the Manual: Overlay (IMG_EFFECT_OVERLAY) has the effect that black background pixels will remain black, white background pixels will remain white, but grey background pixels will take the colour of the foreground pixel.

In this example the black and white rectangles are in the background but the ellipses do not show on those colors.

If you use IMG_EFFECT_NORMAL before the green ellipse, it shows as normal alpha on all background colors.

imagelayereffect($image, IMG_EFFECT_ALPHABLEND);
This feature also works on colors with no alpha channel (but the background must be gray). A lighter gray gives you a lighter shade of color blend. Here we compare imagecolorallocate($image, 155,155,155) with imagecolorallocate($image,200,200,200)
There are 2 more flags for alpha blending. You may see these in scripts that involve imagecopymerge or imagecopyresampled. The flags are imagealphablending($image,true/false) and imagesavealpha($image, true/false). From the manual:

imagealphablending() allows for two different modes of drawing on truecolor images. In blending mode, the alpha channel component of the color supplied to all drawing function, such as imagesetpixel() determines how much of the underlying color should be allowed to shine through. As a result, gd automatically blends the existing color at that point with the drawing color, and stores the result in the image. The resulting pixel is opaque. In non-blending mode, the drawing color is copied literally with its alpha channel information, replacing the destination pixel. Blending mode is not available when drawing on palette images. Whether to enable the blending mode or not. On true color images the default value is TRUE otherwise the default value is FALSE. imagesavealpha() sets the flag to attempt to save full alpha channel information (as opposed to single-color transparency) when saving PNG images.


Right click, select all, ctrl+c to copy.
As from the manual above "imagesavealpha() sets the flag to attempt to save full alpha channel information (as opposed to single-color transparency) when saving PNG images."

The 2 images above show imagesavealpha($image,false); in the top images while the bottom uses imagesavealpha($image,true); It appears to me that this flag applies to the background as well as the alpha colors. You only need to use the flag once in a script to affect the entire script. You can't turn it on and off. However, you can turn imagealphablending($im,true/false); on and off in the script. The top and middle yellow squares in the image are set to true while the 3rd square is set to false.