问题描述
尝试使用GD库显示字体.确实有一个图像,只是没有任何显示.
Trying to display a font using the GD library. There is indeed an image there, it's just that theres nothing displaying.
PHP:
header('Content-Type: image/png');
$font = $_GET['font'];
// Create the image
$image = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$grey = imagecolorallocate($image, 128, 128, 128);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, 399, 29, $white);
// The text to draw
$text = 'The Quick Brown Fox Jumps over the Lazy Dog';
$font = '/Aller/' . $font;
// Add the text
imagettftext($image, 20, 0, 10, 20, $black, $font, $text);
imagepng($image);
HTML:
<img src="fontgen.php?font=Aller_Rg.ttf" alt="" />
字体驻留在fonts/Aller/Aller_Rg.tff
The font resides in fonts/Aller/Aller_Rg.tff
我在做什么错了?
推荐答案
问题似乎出在 $ font
变量上.从文档中:
The problem seems to be the $font
variable. From the documentation:
当使用低于2.0.18的GD库版本时,空格字符(而不是分号)被用作不同字体文件的路径分隔符".意外使用此功能将导致出现警告消息:警告:找不到/打开字体.对于这些受影响的版本,唯一的解决方案是将字体移动到不包含空格的路径.
When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.
在许多情况下,字体与使用该脚本的脚本位于同一目录中,以下技巧可减轻任何包含问题.
In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.
<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>
您还说过,该字体位于 fonts/Aller/
目录中.而在您的脚本中,没有对 fonts
目录的引用.
You also said that the font resides in fonts/Aller/
directory. Whereas, in your script, there is no reference to the fonts
directory.
这篇关于PHP imagettftext()-什么都没有显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!