Current Location: Home> Latest Articles> Use PHP to implement translucent watermark function

Use PHP to implement translucent watermark function

M66 2025-05-22

In web development, we often need to add watermarks to images, such as overlaying trademarks, text or transparent layers on product images to prevent images from being stolen or used for brand identity. In PHP, the GD library provides rich image processing functions, and imagecolorallocatealpha is one of the keys to implementing transparent colors.

This article will explain in detail how to use the imagecolorallocatealpha function to add a translucent text watermark to the image.

What is imagecolorallocatealpha?

imagecolorallocatealpha is a function in the PHP GD library that is used to assign a color on the image and can specify its transparency (alpha value). Its function definition is as follows:

 int imagecolorallocatealpha ( resource $image , int $red , int $green , int $blue , int $alpha )

in:

  • $image : Target image resource.

  • $red, $green, $blue : RGB component of color (0–255).

  • $alpha : Transparency, with values ​​ranging from 0 (completely opaque) to 127 (completely transparent).

Note: This is different from the concept of alpha in CSS. The alpha of CSS is 0 to be transparent, while the 0 here means opaque.

Example: Add translucent text watermark to the image

Next, we use a complete example to add a translucent text watermark to an image.

1?? Prepare a picture

Suppose we have an image: https://m66.net/images/sample.jpg .

2?? Write PHP code

 <?php
// Load the original image
$imagePath = 'https://m66.net/images/sample.jpg';
$image = imagecreatefromjpeg($imagePath);

// Setting font files(Make sure there is this file on the server)
$fontFile = './arial.ttf';  // Please use the actual one ttf Font file path

// Assign translucent colors(White,transparency 50%)
$whiteAlpha = imagecolorallocatealpha($image, 255, 255, 255, 63); // alpha 63 ≈ translucent

// Add text watermark
$text = 'm66.net';
$fontSize = 20;
$x = 20;
$y = 50;
imagettftext($image, $fontSize, 0, $x, $y, $whiteAlpha, $fontFile, $text);

// Output image to browser
header('Content-Type: image/jpeg');
imagejpeg($image);

// Destroy image resources
imagedestroy($image);
?>

Code key analysis

? Loading pictures <br> We use imagecreatefromjpeg to load a JPEG image, you can also use imagecreatefrommpng or imagecreatefromgif as needed.

? Assign colors with transparency
imagecolorallocatealpha($image, 255, 255, 255, 63) is assigned a white translucent color. 63 is approximately 50% transparency (127/2).

? Add text
imagettftext is used to draw text in TrueType font on an image, and the font file path is required.

? Output and Destruction <br> We set the response type through header('Content-Type: image/jpeg') , and then use imagejpeg to output the image to the browser. Finally, use imagedestroy to free memory.

Things to note

  • The GD library must be enabled : Make sure that the GD library is installed and enabled in your PHP environment.

  • The font file path is correct : imagettftext requires a font file (.ttf). If the path is wrong, the text will not be displayed.

  • Save to file : If you do not want to output directly to the browser, you can save it to the file using imagejpeg($image, 'output.jpg') .

summary

With imagecolorallocatealpha , we can easily assign transparent colors to images to achieve various transparent effects. When combining functions such as imagettftext or imagefilledrectangle , professional translucent watermarks can be created, which are both beautiful and practical.

Next time you need to protect your pictures, you might as well try using PHP to generate your own watermark! If you need a complete code example or more watermark tips, you can leave a message and I will continue to share more in-depth content.