Skip to content
PHP Logo

Can PHP GDlib support soft transparency in text with PNG format?

Yes, PHP GDlib can support soft transparency in text with PNG format by using the `imagecolorallocatealpha()` function to allocate a color with an alpha channel, and then setting the alpha channel value for the text with `imagettftext()`. This allows for creating text with soft transparency in PNG images.

php
<?php
// Create a blank image with transparent background
$image = imagecreatetruecolor(200, 100);
imagesavealpha($image, true);
$transColor = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transColor);

// Set the text color with alpha channel
$textColor = imagecolorallocatealpha($image, 255, 255, 255, 63);

// Add text with soft transparency
imagettftext($image, 20, 0, 10, 50, $textColor, 'arial.ttf', 'Hello World');

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

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