Current Location: Home> Latest Articles> Add translucent color effects to text

Add translucent color effects to text

M66 2025-05-20

In PHP, the GD library is a very powerful tool set when processing images and generating image content. imagecolorallocatealpha is a function used in the GD library to assign colors to images. It not only allows us to set the red, green and blue components of the color, but also supports the alpha channel, that is, transparency. This function is often used to draw text, shapes, or graphics with transparent effects on images.

In this article, we will learn how to use the imagecolorallocatealpha function to add a translucent color effect to text.

1. Preparation

First, make sure that the GD library is enabled in your PHP environment. If not enabled, it can be enabled in the php.ini file or installed via the following command:

 sudo apt-get install php-gd

Once enabled, restart the web server.

Next, we need a simple PHP file to test our approach.

2. Overview of imagecolorallocatealpha function

The imagecolorallocatealpha function is used to assign colors to images, and its prototype is as follows:

 int imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha);
  • $image : Image resource, usually created by imagecreate or imagecreatetruecolor .

  • $red , $green , $blue : RGB components of color.

  • $alpha : Transparency, with a value range of 0 (completely opaque) to 127 (completely transparent).

3. Create an image and use translucent colors

Next, we will create a simple image and use imagecolorallocatealpha to add a translucent color effect to the text.

Sample code:

 <?php
// Create a blank image,Size is400x200
$image = imagecreatetruecolor(400, 200);

// Assign a color to the background(White)
$background_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $background_color);

// Set the font color to translucent red(RGB: 255, 0, 0,Alpha: 60)
$text_color = imagecolorallocatealpha($image, 255, 0, 0, 60);

// Add text to image
$text = "Hello, World!";
$font = 5; // PHPBuilt-in font size(1-5)
$font_x = 100;
$font_y = 80;
imagestring($image, $font, $font_x, $font_y, $text, $text_color);

// Set content type to picture
header("Content-Type: image/png");

// Output image
imagepng($image);

// Free memory
imagedestroy($image);
?>

4. Explain the code

  • We first created a blank image of size 400x200.

  • Then use imagecolorallocate to assign white to the background. The imagefill function fills this color throughout the canvas.

  • Next, we assign a translucent red to the text using imagecolorallocatealpha . The alpha value here is set to 60, which means that the text will have a certain degree of transparency, not completely opaque.

  • We use the imagestring function to draw text onto an image. You can adjust the font size and text position as needed.

  • Finally, the image is output through the imagepng function and the memory is freed using imagedestroy .

5. Display effect

When you execute the above code, you will see an image with translucent red text on the background with a white background. The transparent effect will make the text more softer, especially when overlapping with other background elements, which can show a certain sense of background perspective.

6. Summary

Using the imagecolorallocatealpha function to add translucency to text in PHP is very simple. You only need to adjust the alpha value to achieve different degrees of transparency. In this way, you can create more flexible and beautiful images, suitable for scenes such as image watermarks, UI elements, or graph displays.