effeee effeee - 2 months ago 13
PHP Question

php gd vertical text an image

hello to all I would like to put on a picture in vertical text in PHP

function scrivi($scrivi,$p) {
$imgResource = imagecreatefromjpeg($p);
$textcolor = imagecolorallocate($imgResource, 255,255, 255); $fontPath="st.ttf";
$fontSize="18";
$rotation="270";//counter-clockwise rotation
$text="this is a text";
$textCoords=imagettfbbox($fontSize , $rotation , $fontPath , $text );
$x= 36 ;
$y= 36;
imagettftext($imgResource, $fontSize, $rotation, $x, $y, $textcolor,$fontPath,$text);
unlink($p);
imagejpeg($imgResource,$p,100);
imagedestroy($imgResource);
}


It works well only that I would like that the letters are turned this is an example using the function

enter image description here

Instead I would like to

enter image description here

edit:
an idea could be to wrap each letter

Answer

All you really need to do is split the text into an array, loop it, then offset the y by the height + leading of the font character:

function scrivi($p,$text)
    {
        $imgResource    =   imagecreatefromjpeg($p);
        $textcolor      =   imagecolorallocate($imgResource, 255,255, 255);
        $fontPath       =   __DIR__."/st.ttf";
        $fontSize       =   "18";
        $x  =   36 ;
        $y  =   36;
        foreach(str_split($text) as $char) {
            $textCoords =   imagettfbbox($fontSize, 0, $fontPath, $char);
            imagettftext($imgResource, $fontSize, 0, $x, $y, $textcolor,$fontPath,$char);
            $y  +=  24;
        }
        unlink($p);
        imagejpeg($imgResource,$p,100);
        imagedestroy($imgResource);
    }

scrivi('http://imgtops.sourceforge.net/bakeoff/bw.jpg',"Cats are great");

Gives you:

enter image description here

(Image credit: http://imgtops.sourceforge.net/bakeoff/)