Current Location: Home> Latest Articles> How to add transparent colors to PNG images

How to add transparent colors to PNG images

M66 2025-05-18

In PHP, the imagecolorallocatealpha function is used to assign colors to images and supports setting transparency for images. This function is often used to process PNG images because the PNG format supports transparency. By using this function, you can set a color with transparency for pixels in the image, creating an image with a transparent background or partially transparent elements.

Function Overview

The imagecolorallocatealpha function is defined as follows:

 int imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha)
  • $image : The target image resource, usually an image resource created by functions such as imagecreatefrommpng() or imagecreatetruecolor() .

  • $red , $green , $blue : The RGB component of the color, with values ​​ranging from 0 to 255.

  • $alpha : The transparency value of the color, ranging from 0 to 127. 0 means completely opaque and 127 means completely transparent.

Sample code

Here is a simple example showing how to add transparent colors to a PNG image using the imagecolorallocatealpha function.

 <?php
// Load a PNG image
$image = imagecreatefrompng('input.png');

// Assign a color with transparency
$red = 255;
$green = 0;
$blue = 0;
$alpha = 63; // Translucent red

$transparentRed = imagecolorallocatealpha($image, $red, $green, $blue, $alpha);

// 在image中绘制一个半透明的矩形
imagefilledrectangle($image, 50, 50, 200, 200, $transparentRed);

// 设置image为 PNG Format,Save the output
imagesavealpha($image, true); // make sure alpha Channel saving
imagepng($image, 'output.png');

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

Code parsing

  1. Loading the image : We first load a PNG image through the imagecreatefrommpng() function. If your image is in another format (such as JPEG), you can use imagecreatefromjpeg() .

  2. Assign color : Through the imagecolorallocatealpha function, we create a red and set its transparency to 63 (i.e. semi-transparent).

  3. Drawing the graph : Use the imagefilledrectangle function to draw a translucent rectangle on the image.

  4. Save the image : Save the processed image in PNG format through imagepng() and ensure that transparency information is retained (through imagesavealpha() function).

  5. Free memory : Finally, we release image resources through imagedestroy() .

Things to note

  • Transparency is very important in PNG images, especially when you want the image to be partially transparent or without background. The PNG format supports transparent channels (alpha channels), but the JPG format does not.

  • The smaller the transparency value, the more opaque the color; the larger the transparency value, the more transparent the color.

  • The difference between imagecolorallocatealpha and imagecolorallocate is that the latter does not support transparency and is only suitable for images that do not require transparency.

Sample Application

Suppose you have an icon or logo and want its background to be transparent, or create a translucent effect in some areas. These effects can be easily achieved using imagecolorallocatealpha , especially suitable for graphics processing and generation in web development.