使用 PHP GD 计算文本宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11696920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:53:49  来源:igfitidea点击:

Calculating Text Width with PHP GD

phpfontsgd

提问by Colin

I'm simply trying to get the width of a dynamic line of text for addition to an image generated with GD PHP. I'm a little unsure how though. I know how to load a font using imageloadfont(), but can I use a .ttf file? I want to know the width of text using size 12 arial font. When I try to use my ttf file I get the error "Error reading font, invalid font header." If I need a .gdf file, where can I find a font size 12 gdf file? Here's my code:

我只是想获得动态文本行的宽度,以添加到使用 GD PHP 生成的图像中。我有点不确定如何。我知道如何使用 imageloadfont() 加载字体,但是我可以使用 .ttf 文件吗?我想知道使用 12 号 arial 字体的文本宽度。当我尝试使用我的 ttf 文件时,我收到错误“读取字体错误,字体标题无效”。如果我需要 .gdf 文件,在哪里可以找到字体大小为 12 的 gdf ​​文件?这是我的代码:

$newfont = imageloadfont("../fonts/arial.ttf");
$font_width = imagefontwidth($newfont);
$font_height = imagefontheight($newfont);

回答by dazedviper

imageloadfont()is used to load user-defined bitmaps. If you just want to use Arial or any other TrueType fonts (.ttf) or OpenType fonts (.otf) (support for the latter in GD lib is buggy), then what you need is imagettftext(). Before using imagettftext()and writing text to your image though, you first need to know if it will fit. To know this you just need to call imagettfbbox()and pass it the font size, the angle of the text (0 for horizontal text), the path to your .ttf or .otf font file and the string of text itself and it will return an array with 8 elements representing four points making the bounding box of the text (check PHP manual for specifics). You can then reference those array elements and perform calculations in order to know the width and height that that particular string of text will take up. You can then use those values to create an image with a specific width and height that will allow for the text to be displayed in its entirety.

imageloadfont()用于加载用户定义的位图。如果您只想使用 Arial 或任何其他 TrueType 字体 (.ttf) 或 OpenType 字体 (.otf)(GD 库中对后者的支持有问题),那么您需要的是imagettftext(). 但是,在使用imagettftext()图像并将文本写入图像之前,您首先需要知道它是否适合。要知道这一点,你只需要打电话imagettfbbox()并将字体大小、文本的角度(水平文本为 0)、.ttf 或 .otf 字体文件的路径以及文本本身的字符串传递给它,它将返回一个包含 8 个元素的数组,代表四个点,使文本的边界框(查看 PHP 手册了解详细信息)。然后,您可以引用这些数组元素并执行计算,以了解该特定文本字符串将占用的宽度和高度。然后,您可以使用这些值创建具有特定宽度和高度的图像,以允许完整显示文本。

Here is a simple script that accomplishes what you are trying to do in order to get you started:

这是一个简单的脚本,它可以完成您正在尝试做的事情,以便让您入门:

<?php # Script 1

/*
 * This page creates a simple image.
 * The image makes use of a TrueType font.
 */

// Establish image factors:
$text = 'Sample text';
$font_size = 12; // Font size is in pixels.
$font_file = 'Arial.ttf'; // This is the path to your font file.

// Retrieve bounding box:
$type_space = imagettfbbox($font_size, 0, $font_file, $text);

// Determine image width and height, 10 pixels are added for 5 pixels padding:
$image_width = abs($type_space[4] - $type_space[0]) + 10;
$image_height = abs($type_space[5] - $type_space[1]) + 10;

// Create image:
$image = imagecreatetruecolor($image_width, $image_height);

// Allocate text and background colors (RGB format):
$text_color = imagecolorallocate($image, 255, 255, 255);
$bg_color = imagecolorallocate($image, 0, 0, 0);

// Fill image:
imagefill($image, 0, 0, $bg_color);

// Fix starting x and y coordinates for the text:
$x = 5; // Padding of 5 pixels.
$y = $image_height - 5; // So that the text is vertically centered.

// Add TrueType text to image:
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_file, $text);

// Generate and send image to browser:
header('Content-type: image/png');
imagepng($image);

// Destroy image in memory to free-up resources:
imagedestroy($image);

?>

Change values accordingly to fit your needs. Don't forget to read the PHP manual.

相应地更改值以满足您的需要。不要忘记阅读 PHP 手册。

回答by Sentence

With GD2, the imagettfbbox's font size need to be in PTs, not in Pixels with the following convert:

使用 GD2,imagettfbbox的字体大小需要以 PT 为单位,而不是以像素为单位,并进行以下转换:

($fontSizeInPixel * 3) / 4

($fontSizeInPixel * 3) / 4