PHP provides a rich GD library function to manipulate pixels, colors, and transparency when working with dynamically generated images. Among them, the imagecolorallocatealpha() function is a very useful tool that can be used to assign colors with transparency to images. This is especially important for the need to overlay translucent watermarks, create shadow effects, or generate images with transparent areas.
This article will show you how to use imagecolorallocatealpha() and combine sample code to demonstrate how to dynamically adjust the transparency of elements in an image.
The definition of imagecolorallocatealpha() is as follows:
int imagecolorallocatealpha ( resource $image , int $red , int $green , int $blue , int $alpha )
$image : Image resource handle, created by imagecreatetruecolor() .
$red , $green , $blue : RGB components of color, range 0–255.
$alpha : Transparency, range 0 (completely opaque) to 127 (completely transparent).
Note : PHP's alpha value and usual transparency definition are the opposite, the larger the value, the more transparent it is.
Here is a simple example showing how to draw a circle with adjustable transparency in a dynamic image using imagecolorallocatealpha() .
<?php
// Set content header,Browser output PNG picture
header('Content-Type: image/png');
// Create a 200x200 True colored canvas
$width = 200;
$height = 200;
$image = imagecreatetruecolor($width, $height);
// Enable alpha Channel saving
imagesavealpha($image, true);
// Create a完全透明的背景
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparent);
// Dynamically set transparency(Here we use GET Parameter control)
$alpha = isset($_GET['alpha']) ? intval($_GET['alpha']) : 50;
if ($alpha < 0) $alpha = 0;
if ($alpha > 127) $alpha = 127;
// Assign a red with transparency
$redAlpha = imagecolorallocatealpha($image, 255, 0, 0, $alpha);
// Draw a circle in the center of the canvas
imagefilledellipse($image, $width / 2, $height / 2, 150, 150, $redAlpha);
// Output image
imagepng($image);
// Destroy image resources
imagedestroy($image);
?>
Access Example:
https://m66.net/transparent_circle.php?alpha=30
You can dynamically see the change in transparency in the browser by adjusting the alpha parameters (0–127).
Enable alpha channel <br> Using imagesavealpha($image, true) is required, otherwise the transparent area may be filled in black.
Background filled with transparent color <br> To ensure that the transparent part of the image is not overwritten by the default background, we first fill the background with a completely transparent color.
Transparency direction <br> Unlike CSS, the greater the transparency value in GD, the more transparent it is. For example, 0 is completely opaque, while 127 is completely transparent.
Generate translucent overlays for dynamic watermarks.
Creates text with shadow or halo effects.
Generate transparent PNG for web pages or applications.
By combining imagecolorallocatealpha() and other GD functions (such as imagestring() and imagettftext() ), you can build complex dynamic image effects to improve the visual expression of the website.