duanjiao5723 2016-10-08 03:40
浏览 67
已采纳

php gd垂直文本图像

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

an idea could be to wrap each letter

  • 写回答

1条回答 默认 最新

  • doukun5339 2016-10-08 05:21
    关注

    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/)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #求带 python,使用pycharm爬取智联招聘数据资源并保存为Excel文件,同时以图表形式呈现
  • ¥15 学校优化算法怎么样,国内用的多吗?
  • ¥15 微信购物商城购物车的所有功能以及购物车,和首页的搜索功能
  • ¥40 servlet的web程序部署出错
  • ¥50 activiti 新建流程系列问题
  • ¥50 为什么我版本升级之后运行速度变慢5倍??
  • ¥15 如何在gazebo中加载机械臂和机械手
  • ¥15 纯运放实现隔离采样方案设计
  • ¥20 easyconnect无法连接后缀带.com的网站,一直显示找不到服务器ip地址,但是带有数字的网站又是可以连接上的,如何解决
  • ¥15 电脑开机过商标后就直接这样,求解各位