我真的很感谢任何比我有更多 PHP 经验的人的帮助。
我正在使用 PHP 生成紧密包装的文本图像。到目前为止,它是这样工作的:
- 首先,它生成 x 个字符的随机字符串。
- 然后它制作带有背景等的图像。
- 它将第一个字符放置在特定位置(大小随机化,旋转等)
然后它放置下一个字符,使其边界框的左下角与前一个字符的边界框的右下角位于同一点(加上一些随机性等)。代码如下:
$coords = array(); $pos_y = $this->image_height / 2; for ($counter = 0; $counter < $this->code_length; $counter++){ $sbox = @imagettfbbox($size, $angle, $font, $this->code[$counter]); if($counter == 0) $pos_x = $this->image_width / $this->code_length; else // element 2 is the x co-ord of the bottom right corner of the ttf box $pos_x = $coords[$counter-1][2]; @imagettftext($this->image, $size, $angle, $pos_x, $pos_y, $fontcolor, $font, $this->code[$counter]); }
并为字符串中的每个后续字符重复 4。
我想要的是尽可能地让每个角色都接触到它的邻居。到目前为止,我所做的对于像“M”这样的字符来说效果很好,其中字形的底角或多或少位于边界框的角落,但对于像“G”或“I”这样的字母来说,它还差得远。
有谁知道获取 ttf 字形的实际尺寸而不仅仅是边界框的方法?或者有人能想出更好的方法来解决这个问题吗?
非常感谢任何想法/想法/建议。