Question

I have this code to write some text on a new created image:

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


}

and i get just the transparent background none of the text is showed. I am new at PHP GD and i can't figure out why isn't writing the text. Please help me figure out

Thanks.

Was it helpful?

Solution

The coordinates returned by imagettfbbox are relative to the basepoint, and as such, can be negative.

So when the angle is 0, the upper Y and left X are negative, and you need to subtract them from the lower Y and right X to get the box size.

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

And then you have then to use the absolute values of the negative coordinates as the basepoint coordinate for drawing the text:

imagettftext($image, 20, 0, -$txtsize[0], -$txtsize[5], $black, $font, $string);

OTHER TIPS

Do you have warnings enabled? There could be an issue with PHP locating the font file, since I noticed it is not a complete path.

Does your server have the FreeType library installed? Use phpinfo() to verify. http://www.freetype.org/

You can use this sandbox to test if your coordinates are right: http://ruquay.com/sandbox/imagettf/.

Remember, the basepoint is the x, y coordsinates you use to draw text with imagettftext. A useful thing to do is take a string like...

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

and use the "aboveBasepoint" value for the height of your font. Now you can draw lines and use "the height of your font * leading" as the distance between lines of text, where leading is a number such as 1.45 (for 45% leading).

Look in the comments at the bottom of the PHP Manual entry for this function for many useful functions: http://php.net/manual/en/function.imagettfbbox.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top