简体   繁体   English

php gd垂直文本图像

[英]php gd vertical text an image

I would like to put on a picture in vertical text in PHP: 我想在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 仅在我希望将字母转成字母的情况下才有效,这是使用函数的示例

在此处输入图片说明

Instead I would like to 相反,我想

在此处输入图片说明

an idea could be to wrap each letter 一个想法可能是包装每个字母

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: 您真正需要做的就是将文本分割成一个数组,将其循环,然后将y偏移字体字符的高度+前导:

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: 给你:

在此处输入图片说明

(Image credit: http://imgtops.sourceforge.net/bakeoff/ ) (图片来源: http : //imgtops.sourceforge.net/bakeoff/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM