用PHP GD计算文本宽度 [英] Calculating Text Width with PHP GD

查看:618
本文介绍了用PHP GD计算文本宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图获得一个动态的文本行的宽度添加到GD PHP生成的图像。我有点不确定。我知道如何使用imageloadfont()加载字体,但我可以使用.ttf文件吗?我想知道使用12号字体的文本的宽度。当我尝试使用我的ttf文件时,出现错误Error reading font,invalid font header。如果我需要一个.gdf文件,我可以在哪里找到一个字体大小为12的gdf文件?这是我的代码:

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


解决方案

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



这是一个简单的脚本,完成你想要做的事情,以便开始:

 <?php#Script 1 

/ *
*此页面创建一个简单的图像。
*图像使用TrueType字体。
* /

//建立图像因子:
$ text ='示例文本';
$ font_size = 12; //字体大小以像素为单位
$ font_file ='Arial.ttf'; //这是你的字体文件的路径。

//检索边界框:
$ type_space = imagettfbbox($ font_size,0,$ font_file,$ text);

//确定图像宽度和高度,为5个像素添加10个像素填充:
$ image_width = abs($ type_space [4] - $ type_space [0])+ 10;
$ image_height = abs($ type_space [5] - $ type_space [1])+ 10;

//创建图片:
$ image = imagecreatetruecolor($ image_width,$ image_height);

//分配文本和背景颜色(RGB格式):
$ text_color = imagecolorallocate($ image,255,255,255);
$ bg_c​​olor = imagecolorallocate($ image,0,0,0);

//填写图片:
imagefill($ image,0,0,$ bg_c​​olor);

//修正文本的起始x和y坐标:
$ x = 5; // 5个像素的填充。
$ y = $ image_height - 5; //使文本垂直居中。

//将TrueType文本添加到图像中:
imagettftext($ image,$ font_size,0,$ x,$ y,$ text_color,$ font_file,$ text);

//生成并发送图像到浏览器:
header('Content-type:image / png');
imagepng($ image);

//销毁内存中的图像以释放资源:
imagedestroy($ image);

?>

相应地更改值以符合您的需求。不要忘了阅读PHP手册。


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:

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

解决方案

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.

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 GD计算文本宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆