使用GD库和文本时,左侧的空白令人讨厌 [英] Annoying whitespace to the left when using the GD Library and text
问题描述
我正在研究一个类,该类可以创建许多文本输入并将它们放置在彼此的顶部,以使它们左右对齐-像这样:
I'm working on a class, which can create a number of text inputs and position them on top of each other so that they align, left and right - like this:
(来源: vandret.dk )
(source: vandret.dk)
问题是有时,文本位置不正确. 绘制A时,它可以工作.在绘制M(以及许多其他带有垂直笔划的字母-P,I等)时,它会在左侧留一个空格,并在右侧剪裁该句子.如果您看下面的两行,它们应该完美对齐-但事实并非如此.
The problem is that sometimes, the text is not positioned correctly. When drawing A's, it works. When drawing M's (and a number of other letters with a vertical stroke - P's, I's etc) it puts a whitespace to the left and cropping the sentence to the right. If you look at the two lines below, they should align up perfectly - but doesn't.
(来源: vandret.dk )
(source: vandret.dk)
(来源: vandret.dk )
(source: vandret.dk)
如果右键单击并直接查看图像,问题将会更加明显.
If you right-click and view the images directly, the problem will be more obvious.
它是这样的:
- 创建一个空画布,将其虚拟"放置在画布上以测量宽度和高度
- 获取尺寸后,创建一个与文本尺寸匹配的画布.现在,我们有了一个舞台,宽度为800像素
- 缩小句子或单词的宽度以适合其400像素
- 增加另一个句子或单词的y偏移量
- 只要我们要显示句子/单词,就可以再次做同样的事情
My starting point was this and I built this code:
$fontsize = 120;
$size = imagettfbbox($fontsize, 0, "./fonts/".$fontname, $text);
$xsize = abs($size[0]) + abs($size[2]);
$ysize = abs($size[5]) + abs($size[1]);
//print_r($size);
$image = imagecreate($xsize, $ysize);
// Colors
$backgroundcolor = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 155, 142, 138);
imagettftext($image, $fontsize, 0, 0, abs($size[5]), $textcolor, "./fonts/".$fontname, $text);
header("content-type: image/png");
imagepng($image);
,然后,所有调整大小的东西都起作用了.但是为什么文本对齐错误呢?
and then, all of the resizing stuff, which works. But why is the text aligned wrong?
推荐答案
已解决!这是字体问题,而不是GD问题.我在High-Logic FontCreator中打开了TrueType字体,发现其中几个字母的左侧有一个空格.
Solved! It's a font problem, not a GD problem. I opened the TrueType-font inside High-Logic FontCreator and found out that several of the letters have a spacer to the left.
我在程序中使用了一个名为自动度量向导"的函数来剥离左缩进的所有字符,这在几秒钟内就完成了-字体仍然看起来可以接受.像j这样的字母的字距调整似乎有点过头-但这确实可以做到.
I used a function in the program called "Automatic Metrics Wizard" to strip all of the characters of the left indentation, which was done in a matter of seconds - and the font still looks acceptable. The kerning on letters like j looks a bit off - but it will do.
这篇关于使用GD库和文本时,左侧的空白令人讨厌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!