11

背景が透明な GDlib で画像を作成するには?

header('content-type: image/png');

$image = imagecreatetruecolor(900, 350);

imagealphablending($image, true);
imagesavealpha($image, true);

$text_color = imagecolorallocate($image, 0, 51, 102);
imagestring($image,2,4,4,'Test',$text_color);

imagepng($image);
imagedestroy($image);

こちらは背景が黒

4

6 に答える 6

30

行を追加

imagefill($image,0,0,0x7fff0000);

の前のどこかimagestringで透明になります。

0x7fff0000次のように分類されます。

alpha = 0x7f
red = 0xff
green = 0x00
blue = 0x00

これは完全に透明です。

于 2011-12-08T21:09:01.020 に答える
13

このようなもの...

$im = @imagecreatetruecolor(100, 25);
# important part one
imagesavealpha($im, true);
imagealphablending($im, false);
# important part two
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
# do whatever you want with transparent image
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "captcha.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
于 2011-12-08T21:25:22.340 に答える
9

アルファが0に設定されたimagefill()割り当てられた色( )でそれを使用して塗りつぶす必要があります。imagecolorallocatealpha()

@mvds が言ったように、「割り当ては必要ありません」。トゥルーカラー イメージ (24 または 32 ビット) の場合、それは単なる整数であるため、その整数を に直接渡すことができますimagefill()

呼び出し時にトゥルーカラー画像のバックグラウンドで PHP が行うことimagecolorallocate()は同じです。計算された整数を返すだけです。

于 2011-12-08T21:20:30.047 に答える
8

これはうまくいくはずです:

$img = imagecreatetruecolor(900, 350);

$color = imagecolorallocatealpha($img, 0, 0, 0, 127); //fill transparent back
imagefill($img, 0, 0, $color);
imagesavealpha($img, true);
于 2015-01-19T15:22:19.833 に答える
1

PNG 画像の問題により、透過画像が得られない場合があります。画像は、次の推奨形式のいずれかである必要があります。

PNG-8 (recommended)
Colors: 256 or less
Transparency: On/Off
GIF
Colors: 256 or less
Transparency: On/Off
JPEG
Colors: True color
Transparency: n/a

imagecopymerge 関数は、PNG-24 イメージを適切に処理しません。したがって、お勧めしません。

Adobe Photoshop を使用して透かし画像を作成している場合は、次の設定で [Web 用に保存] コマンドを使用することをお勧めします。

File Format: PNG-8, non-interlaced
Color Reduction: Selective, 256 colors
Dithering: Diffusion, 88%
Transparency: On, Matte: None
Transparency Dither: Diffusion Transparency Dither, 100%
于 2013-06-08T11:13:45.880 に答える