Current Location: Home> Latest Articles> Imagecreatetruecolor() and imagecolorallocatealpha() use examples

Imagecreatetruecolor() and imagecolorallocatealpha() use examples

M66 2025-05-17

In PHP, imagecreatetruecolor() and imagecolorallocatealpha() are two commonly used functions when processing images. These two functions are the key tools to use when you need to create image effects with transparency.

This article will introduce in detail how to use these two functions to create transparent images and explain the key steps and precautions.

1. Introduction to imagecreatetruecolor() function

imagecreatetruecolor() is used to create a true color blank image canvas. The image canvas returned by this function can contain 24-bit colors (red, green, and blue), which makes it suitable for processing high-quality images.

 $width = 200; // The width of the image
$height = 200; // The height of the image

$image = imagecreatetruecolor($width, $height); // Create a blank true color image

2. Introduction to imagecolorallocatealpha() function

imagecolorallocatealpha() is used to assign a color with transparency. Unlike the normal imagecolorallocate() , imagecolorallocatealpha() allows you to set transparency values ​​for colors. The value of transparency ranges from 0 to 127, 0 means completely opaque, and 127 means completely transparent.

 $transColor = imagecolorallocatealpha($image, 255, 255, 255, 50); // Create a translucent white

3. Use both: Create an image with transparency

Next, suppose we want to create an image with a transparent background, we can combine imagecreatetruecolor() and imagecolorallocatealpha() and fill in the background with the imagefill() function.

 <?php
// 设置The width of the image和高度
$width = 200;
$height = 200;

// Create a true color image
$image = imagecreatetruecolor($width, $height);

// Set transparent background of image
imagesavealpha($image, true); // Enable Save alpha aisle
$transColor = imagecolorallocatealpha($image, 255, 255, 255, 127); // Completely transparent white

// The background color is transparent
imagefill($image, 0, 0, $transColor);

// Draw something else in the image(For example, add a translucent rectangle)
$rectColor = imagecolorallocatealpha($image, 255, 0, 0, 50); // Translucent red
imagefilledrectangle($image, 50, 50, 150, 150, $rectColor);

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

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

4. Code parsing

  • Create an image : imagecreatetruecolor() creates an image of 200x200 pixels.

  • Enable transparency support : imagesavealpha($image, true) tells PHP that the image will support the alpha channel, that is, transparency information.

  • Setting a transparent background : imagecolorallocatealpha() creates a completely transparent white that fills the background of the entire image.

  • Draw a translucent rectangle : imagecolorallocatealpha() is again used to create a translucent red. Next, imagefilledrectangle() draws a transparent rectangle on the image.

  • Output image : Finally, the image is output to PNG format via imagepng() , which naturally supports transparent backgrounds.

5. Things to note

  • Image format : PNG format supports transparency. If you try to use transparency in JPEG or GIF formats, it will result in unexpected results.

  • Color and transparency values : imagecolorallocatealpha() has transparency ranging from 0 (completely opaque) to 127 (completely transparent). Transparency can be adjusted as needed.

  • Performance : More computing and memory is required when operating transparent images, especially when the images are large, so you should pay attention to performance issues in actual use.

6. Conclusion

By using imagecreatetruecolor() and imagecolorallocatealpha() together, PHP gives us the flexibility to create images with transparent effects. Whether in image processing, image editing or graphic design, transparency can provide us with more creative space. I hope this article can help you understand how these two functions are used and provide assistance to your future image processing work.