doubutao6216 2017-07-27 19:23
浏览 23
已采纳

在PHP中编写包含多种字体的文本

I am currently using Intervention class to write a text on images.

writing is easily possible using text() method:

$img->text('The quick brown fox jumps over the lazy dog.', 120, 100);

Documentation

but my problem is that I want each character of a word with a different font. for example, imagine the word test, each character must use a different font. this is not easily possible since I can't find the position to start the next character, for instance in the word test, when I write t then when I want to call the text() method to write e, I don't know where should I set the position so the character e goes right after t.

How can I get the position of the last character so I can start writing the next character where the last one ends? or is there any other way to achieve this?

  • 写回答

1条回答 默认 最新

  • doujie4344 2017-07-28 07:30
    关注

    This is the method I use to get the width of a string.
    I use imagettftext(), so it may not suit you. But this code works.
    So if you have to choose between nothing and a code that works...

    $type_space = imagettfbbox($size, 0, $font, $text); // 0 = angle
    $text_width = abs($type_space[4] - $type_space[6]);
    

    imagettfbbox returns an array of values.
    http://php.net/manual/en/function.imagettfbbox.php

    Imagettfbbox() returns an array with 8 elements representing four points making the bounding box of the text:
    0 lower left corner, X position
    1 lower left corner, Y position
    2 lower right corner, X position
    3 lower right corner, Y position
    4 upper right corner, X position
    5 upper right corner, Y position
    6 upper left corner, X position
    7 upper left corner, Y position

    The code takes upper right x - upper left x which should mean width.

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

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi