使用PHP的GD库中的imagepng()和透明度
问题描述:
在PHP中使用函数imagepng()
时,如何确保保存的图像带有透明背景?
When using the function imagepng()
in PHP, how can I make sure the images that I save are saved with a transparent background?
答
Here is an example of the imagecolortransparent
function (if it helps):
<?php
// Create a 55x30 image
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
// Draw a red rectangle
imagefilledrectangle($im, 4, 4, 50, 25, $red);
// Save the image
imagepng($im, './imagecolortransparent.png');
imagedestroy($im);
?>