effeee effeee - 1 year ago 129
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";
$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);

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

an idea could be to wrap each letter

Answer Source

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;

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

Gives you:

enter image description here

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download