通过gd制作透明的png图像

时间:2011-07-24 10:27:29

标签: php gd

我想制作透明的png图像,并用imagefttext(黑色)打印字符串。 我读了一些例子,我想我应该使用imagecolortransparent函数,但它通过黑色背景制作图像。 我应该怎么做?

1 个答案:

答案 0 :(得分:6)

试试这个

<?php
//Canvas size 100x50
$image = imagecreatetruecolor(100, 50);
imagealphablending($image, false);
//Create alpha channel for transparent layer
$col=imagecolorallocatealpha($image,255,255,255,127);
//Create overlapping 100x50 transparent layer
imagefilledrectangle($image,0,0,100, 50,$col);
//Continue to keep layers transparent
imagealphablending($image,true);
//Insert the text
imagefttext($image,10,0,10,20,0,'octin.ttf','test sting');
//Keep trnsparent when saving
imagesavealpha($image,true);

//Save & output
if(imagepng($image, "test.png", 1)){
   header("Content-Type: image/png");
   readfile('test.png');
}
imagedestroy($image);
?>

输出100x50px [test.png]

Output

哎呀,我忘记了r ...我的不好