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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?