Current Location: Home> Latest Articles> Use imagecolorallocatealpha() and imagefilledrectangle() to achieve transparent masking effect

Use imagecolorallocatealpha() and imagefilledrectangle() to achieve transparent masking effect

M66 2025-05-24

In PHP, we can use the image processing library GD to perform image editing, cropping, adding text, drawing graphics and other operations. Today we will explore how to use the imagecolorallocatealpha() and imagefilledrectangle() functions to add transparent masking effects to images.

1. imagecolorallocatealpha() function

imagecolorallocatealpha() is a function used to assign colors and support transparency. It creates colors with transparency (alpha channel) when generating images.

The function prototype is as follows:

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

  • $red : red component, value range 0-255.

  • $green : Green component, value range 0-255.

  • $blue : blue component, value range 0-255.

  • $alpha : Transparency, with a value range of 0-127, where 0 represents total opaque and 127 represents complete transparency.

2. imagefilledrectangle() function

imagefilledrectangle() is used to draw a filled rectangle, often used to add background colors or cover part of the image.

The function prototype is as follows:

 bool imagefilledrectangle(resource $image, int $x1, int $y1, int $x2, int $y2, int $color);
  • $image : Image resource.

  • $x1, $y1 : The starting point coordinate of the rectangle.

  • $x2, $y2 : The end point coordinate of the rectangle.

  • $color : The fill color of the rectangle, usually created by the imagecolorallocatealpha() function.

3. Achieve transparent masking effect

We will use imagecolorallocatealpha() and imagefilledrectangle() to achieve the transparent mask effect on the image. Here is a sample code showing how to add a transparent rectangular mask to an image.

Sample code:

 <?php
// Loading pictures
$image = imagecreatefromjpeg('path_to_your_image.jpg');

// Get the width and height of the image
$width = imagesx($image);
$height = imagesy($image);

// Create a transparent grey mask
$maskColor = imagecolorallocatealpha($image, 0, 0, 0, 75);  // 75 It&#39;s transparency,The higher the transparent

// Draw mask rectangle
imagefilledrectangle($image, 50, 50, $width - 50, $height - 50, $maskColor);  // Draw a rectangular mask on the image

// Output image
header('Content-Type: image/png');
imagepng($image);

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

4. Code analysis

  1. Loading pictures:
    Use the imagecreatefromjpeg() function to load images in JPEG format. You can select other functions such as imagecreatefrompng() and imagecreatefromgif() according to actual needs.

  2. Get image width and height:
    Get the width and height of the image through imagesx() and imagesy() , which is very important for determining the size of the mask.

  3. Create transparent colors:
    The imagecolorallocatealpha() function is used to create colors with transparency. This example uses the RGB value (0, 0, 0) to generate black and sets the transparency to 75 (more transparent).

  4. Draw mask:
    Use the imagefilledrectangle() function to draw a fill rectangle on the image with the starting point coordinates (50, 50) and the end point coordinates (width - 50, height - 50) so that you can add a rectangular transparent mask to the image.

  5. Output image:
    Use imagepng() to output the image and set the correct Content-Type header to ensure that the browser correctly recognizes and displays the image.

  6. Destroy image resources:
    Use imagedestroy() to destroy image resources and free up memory.

5. Summary

With the combination of imagecolorallocatealpha() and imagefilledrectangle() , you can easily achieve transparent masking effects of images in PHP. This method is not only suitable for various image formats (such as JPEG, PNG, GIF, etc.), but also allows the transparency and mask position to be adjusted according to actual needs.

I hope this article can help you master the skills of using the GD library to implement transparent masking in PHP and improve your image processing capabilities!