php gd垂直文本图像

时间:2016-10-08 03:40:31

标签: php gd

我想在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);
}

它的效果很好我只希望字母被转动这是一个使用函数

的例子

enter image description here

相反,我想

enter image description here

一个想法可能是包装每个字母

1 个答案:

答案 0 :(得分:0)

您真正需要做的就是将文本拆分为数组,循环播放,然后将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");

给你:

enter image description here

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