7

部分的に透明な画像の上にテキストを配置するのに問題があります。テキストを塗りつぶしたいのですが、画像の背景の一部を透明にしたいのですが、テキストが塗りつぶされている部分を塗りつぶしたいのですが、問題は、テキストがいずれかの透明な背景を継承していることです。前のレイヤー。これがコードと出力の例であり、その出力の下で私が望むものを示します。画像は明るい灰色の背景に配置されているため、暗い灰色の間の画像の周囲の明るい境界線は透明ですが、特にテキストは他にありません。透明なのはテキストそのものではなく、テキストブロックの背景のようです。ご覧のとおり、これはあまり望ましくありません。助けてください、これは私が私のプロジェクトを完了するために残した唯一の問題です。:)

まだ画像を投稿できないので、出力例と望ましい結果の画像へのリンクを次に示します(orig):

ここに画像の説明を入力してください

<?php

$img = imagecreatetruecolor(200, 50);

$imageX = imagesx($img);
$imageY = imagesy($img);

imagealphablending($img, false);
imagesavealpha($img, true);

$transparent = imagecolorallocatealpha($img, 255,255,255, 127);
$white = imagecolorallocate($img, 255,255,255);
$grey = imagecolorallocate($img, 127,127,127);
imagefilledrectangle($img, 0, 0, $imageX, $imageY, $grey);
imagefilledrectangle($img, 2, 2, $imageX-4, $imageY-4, $transparent);

$font = "./arialbd.ttf";
$fontSize = 12;
$text = "THIS IS A TEST";

$textDim = imagettfbbox($fontSize, 0, $font, $text);
$textX = $textDim[2] - $textDim[0];
$textY = $textDim[7] - $textDim[1];

$text_posX = ($imageX / 2) - ($textX / 2);
$text_posY = ($imageY / 2) - ($textY / 2);

imagefilledrectangle($img, 10, 10, $imageX-10, $imageY-10, $grey);
imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text);

header("Content-Type: image/png");
imagepng($img);

?>
4

1 に答える 1

19

ハァ、私はそれについて十分に考えていなかったと思います。解決策は、画像にテキストを配置する前に imagealphablending をオンに戻すことでした。

<?php

$img = imagecreatetruecolor(200, 50);

$imageX = imagesx($img);
$imageY = imagesy($img);

imagealphablending($img, false);
imagesavealpha($img, true);

$transparent = imagecolorallocatealpha($img, 255,255,255, 127);
$white = imagecolorallocate($img, 255,255,255);
$grey = imagecolorallocate($img, 127,127,127);
imagefilledrectangle($img, 0, 0, $imageX, $imageY, $grey);
imagefilledrectangle($img, 2, 2, $imageX-4, $imageY-4, $transparent);

$font = "./arialbd.ttf";
$fontSize = 12;
$text = "THIS IS A TEST";

$textDim = imagettfbbox($fontSize, 0, $font, $text);
$textX = $textDim[2] - $textDim[0];
$textY = $textDim[7] - $textDim[1];

$text_posX = ($imageX / 2) - ($textX / 2);
$text_posY = ($imageY / 2) - ($textY / 2);

imagefilledrectangle($img, 10, 10, $imageX-10, $imageY-10, $grey);
imagealphablending($img, true);
imagettftext($img, $fontSize, 0, $text_posX, $text_posY, $white, $font, $text);

header("Content-Type: image/png");
imagepng($img);

?>
于 2010-01-25T18:36:52.270 に答える