1

新しく作成した画像にテキストを書き込むための次のコードがあります。

class ImageCreator
{
    public function Draw($string)
    {
        $font = "lg.ttf";

        $txtsize = imagettfbbox(20, 0, $font, $string);
        $image = imagecreatetruecolor($txtsize[2], $txtsize[4]);
        imagealphablending($image, false);
        $c = imagecolorallocatealpha($image,0,0,0,127);
        imagefilledrectangle($image,0,0,$txtsize[2],$txtsize[4], $c);
        imagealphablending($image, true);

        $c = ImageCreator::hexcol2dec("#FFFFFF");
        $white = imagecolorallocate($image, $c[0], $c[1], $c[2]);
        $c = ImageCreator::hexcol2dec("#000000");
        $black = imagecolorallocate($image, $c[0], $c[1], $c[2]);

        $c = ImageCreator::hexcol2dec("#044D8F");
        $tcolor = imagecolorallocate($image, $c[0], $c[1], $c[2]);

        imagettftext($image, 20, 0, 0, 0, $black, $font, $string);
        imagettftext($image, 20, 0, 1, 0, $tcolor, $font, $string);
        imagettftext($image, 20, 0, 2, 0, $white, $font, $string);

        imagealphablending($image,false);
        imagesavealpha($image,true);
        ob_start();
        imagepng($image);
        $imgData = ob_get_contents();
        ob_end_clean();
        return $imgData;
    }

    public function hexcol2dec($hexColor)
    {
        $R=0;
        $G=0;
        $B=0;

        for($i = 0; $i < strlen($hexColor);$i++)
        {
            if($hexColor[$i] == "#")
            {

            }
            else if($hexColor[$i] == 'A' || $hexColor[$i] == 'a')
            {
                $dec[$i] = 10;
            }
            else if($hexColor[$i] == 'B' || $hexColor[$i] == 'b')
            {
                $dec[$i] = 11;
            }
            else if($hexColor[$i] == 'C' || $hexColor[$i] == 'c')
            {
                $dec[$i] = 12;
            }
            else if($hexColor[$i] == 'D' || $hexColor[$i] == 'd')
            {
                $dec[$i] = 13;
            }
            else if($hexColor[$i] == 'E' || $hexColor[$i] == 'e')
            {
                $dec[$i] = 14;
            }
            else if($hexColor[$i] == 'F' || $hexColor[$i] == 'f')
            {
                $dec[$i] = 15;
            }
            else
            {
                $dec[$i] = $hexColor[$i];
            }
        }
        if($hexColor[0] == "#")
        {
            $R = 16*$dec[1]+$dec[2];
            $G = 16*$dec[3]+$dec[4];
            $B = 16*$dec[5]+$dec[6];
        }
        else
        {
            $R = 16*$dec[0]+$dec[1];
            $G = 16*$dec[2]+$dec[3];
            $B = 16*$dec[4]+$dec[5];
        }
        return array ($R, $G, $B);
    }


}

透明な背景だけが表示され、テキストは表示されません。私はPHPGDを初めて使用しますが、なぜテキストを書かないのか理解できません。私が理解するのを手伝ってください

ありがとう。

4

2 に答える 2

2

によって返される座標はimagettfbbox基点を基準にしているため、負の値になる可能性があります。

したがって、角度が0の場合、上部のYと左側のXは負であり、ボックスのサイズを取得するには、下部のYと右側のXからそれらを引く必要があります。

$width = $txtsize[2] - $txtsize[0];
$height = $txtsize[1] - $txtsize[5]; 
$image = imagecreatetruecolor($width, $height);

次に、負の座標の絶対値をテキストを描画するための基点座標として使用する必要があります。

imagettftext($image, 20, 0, -$txtsize[0], -$txtsize[5], $black, $font, $string);
于 2012-03-31T13:04:33.073 に答える
0

警告を有効にしましたか?完全なパスではないことに気付いたので、PHPがフォントファイルを見つける際に問題が発生する可能性があります。

サーバーにFreeTypeライブラリがインストールされていますか?確認に使用phpinfo()します。http://www.freetype.org/

このサンドボックスを使用して、座標が正しいかどうかをテストできます:http: //ruquay.com/sandbox/imagettf/

ベースポイントは、でテキストを描画するために使用するx、y座標であることを忘れないでくださいimagettftext。便利なことは、次のような文字列を取得することです...

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

フォントの高さには「aboveBasepoint」の値を使用します。これで、線を描画し、テキストの行間の距離として「フォントの高さ*先頭」を使用できます。先頭は1.45(45%先頭の場合)などの数値です。

多くの便利な関数については、この関数のPHPマニュアルエントリの下部にあるコメントを参照してください:http://php.net/manual/en/function.imagettfbbox.php

于 2012-03-31T01:09:35.373 に答える