6

我几乎可以肯定这不起作用是有一个愚蠢的原因,但我就是想不通。我只是想用 imagettftext 将一些文本打印为图形,但我无法显示这些文字。这是在 GoDaddy 服务器上,所以我不能控制一切,但这里是 phpinfo() 的规范:

  • PHP 版本 5.2.14
  • --with-gd' '--with-freetype-dir=/usr' '--with-jpeg-dir=/usr' '--with-png-dir=/usr/bin/libpng-config' '- -启用-gd-native-ttf'
  • 启用 GD 支持
  • GD 版本捆绑(2.0.34 兼容)
  • 已启用 FreeType 支持
  • FreeType 与 freetype 的链接
  • FreeType 版本 2.2.1

这是我正在使用的代码。没有什么花哨或奇怪的。

$width = 270;
$height = 25;
$image = imageCreate($width, $height);
$white = imageColorAllocate($image, 255, 255, 255);
$black = imageColorAllocate($image, 0, 0, 0);
$font = 'verdana.ttf';
imagefttext($image, 16, 0, 0, 0, $black, $font, 'TESTING TEXT');
header("Content-type:  image/gif");
imageGIF($image);

我尝试以不同的方式更改字体名称:

$font = './verdana.ttf';
$font = dirname(__FILE__).'/verdana.ttf';

我尝试使用 PNG 而不是 GIF,我尝试使用 imagefttext() 和 imagettftext(),我尝试显示错误,但它没有显示任何错误,只是一个空白屏幕。有任何想法吗?一定是这么愚蠢的事情……

4

3 回答 3

9

我明白了(考虑到我是这方面的专家,这让我头疼了一段时间......)

错误是Y 位置必须有字体大小的偏移量,所以它应该看起来像这样

<?php
$width = 270;
$height = 25;
$image = imageCreate($width, $height);
$white = imageColorAllocate($image, 255, 255, 255);
$black = imageColorAllocate($image, 0, 0, 0);
$font = 'verdana.ttf';
imagettftext($image, 16, 0, 0, 16, $black, $font, 'TESTING TEXT');
header("Content-type:  image/gif");
imageGIF($image);
?>
于 2010-09-25T19:20:06.487 回答
1

会不会是你拼错imagettftext了?

于 2010-09-25T18:22:08.167 回答
1
$font = "verdana.ttf";
$im = @imagecreatetruecolor(270, 25)
      or die('Cannot Initialize new GD image stream');
$backg = imagecolorallocate($im,255,255,255);
imagefill($im, 0, 0, $backg);
$color = ImageColorAllocate($im, 0,0,0);
ImageTTFText($im,16,0,0,16, $color,$font,'hello');
header ('Content-type: image/gif');
ImageGIF($im);
ImageDestroy($im);   

试试这个...字体在同一个文件夹中

于 2010-09-25T19:23:33.017 回答