Current Location: Home> Latest Articles> Use imagecolorallocatealpha() on non-truecolor images

Use imagecolorallocatealpha() on non-truecolor images

M66 2025-06-01

In PHP, the imagecolorallocatealpha() function is used to assign images with color with alpha (transparency) information. But it should be noted that this function. If you try to use it on a non-truecolor image (a palette image created by imagecreate() ), you may encounter unexpected behavior or errors.

So, how to use the function of imagecolorallocatealpha() indirectly on non-truecolor images? This article will take you through its principles and provide practical code examples.

1?? Understand truecolor with palette images

  • Palette-based image
    Use up to 256 colors, each color is stored in the palette. Created by imagecreate() . Suitable for simple graphics, but does not support true transparent channels.

  • Truecolor Image <br> Each pixel independently stores RGB (red, green, blue) and alpha (transparency) values. Created by imagecreatetruecolor() . Suitable for complex graphics and scenarios where transparency is required.

Since imagecolorallocatealpha() involves an alpha channel, it requires a truecolor image to take effect.

2?? Solution: Convert to truecolor image first

If you created a palette image with imagecreate() in the beginning, you can convert it to a truecolor image before using imagecolorallocatealpha() .

Sample code

 <?php
// Create a palette image
$paletteImage = imagecreate(200, 200);
$white = imagecolorallocate($paletteImage, 255, 255, 255);
$black = imagecolorallocate($paletteImage, 0, 0, 0);

// Convert palette images to truecolor image
$truecolorImage = imagecreatetruecolor(imagesx($paletteImage), imagesy($paletteImage));
imagecopy($truecolorImage, $paletteImage, 0, 0, 0, 0, imagesx($paletteImage), imagesy($paletteImage));

// Assign a color with transparency
$transparentRed = imagecolorallocatealpha($truecolorImage, 255, 0, 0, 64); // 64 Indicates translucent

// Use this color to draw a fill rectangle
imagefilledrectangle($truecolorImage, 50, 50, 150, 150, $transparentRed);

// Settings Save as PNG(Support transparency)
header('Content-Type: image/png');
imagepng($truecolorImage);

// Clean the memory
imagedestroy($paletteImage);
imagedestroy($truecolorImage);
?>

Things to note

  • Imagecreatetruecolor() creates images with a completely black background by default. If you want to retain the background of the original palette image, you must use imagecopy() .

  • The output is in PNG format to save the alpha channel (JPEG does not support transparency).

  • The transparency value alpha ranges from 0 (completely opaque) to 127 (completely transparent).

3?? Another tip: Use PNG directly as the base image

 <?php
$image = imagecreatefrompng('https://m66.net/images/sample.png');
$semiTransparentBlue = imagecolorallocatealpha($image, 0, 0, 255, 80);
imagefilledellipse($image, 100, 100, 80, 80, $semiTransparentBlue);

header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>