2011-07-24 18 views
0

透明なpngイメージを作成し、それにimagefttext(黒色​​)で文字列を印刷したいと思います。 私はいくつかの例を読んで、私はimagecolortransparent関数を使うべきだと思うが、それは黒い背景でイメージを作る。 どうすればいいですか?gdで透明なpngイメージを作成

答えて

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); 
?> 

出力私はrを忘れてしまった100x50px [test.png]

Output

おっと...私の悪い

+0

素敵を試してみてください、これは素晴らしいです。私はうまくいったが、フォントのrealpathディレクティブを次のように追加しなければならなかった: $ fontpath = realpath( '。'); putenv( 'GDFONTPATH ='。$ fontpath); – rncrtr

関連する問題