【问题标题】:PHP imagefttext (imagettftext) not displaying anythingPHP imagefttext(imagettftext)不显示任何内容
【发布时间】:2011-04-17 05:29:39
【问题描述】:

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

  • PHP 版本 5.2.14
  • --with-gd' '--with-freetype-dir=/usr' '--with-jpeg-dir=/usr' '--with-png-dir=/usr/bin/libpng-config ' '--enable-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(),我尝试过显示错误,但它没有显示任何错误,只是一个空白屏幕。有任何想法吗?一定是很愚蠢的事情......

【问题讨论】:

  • 致所有从搜索引擎来到这里的人:请注意下面@Mark Lalor 的回答,因为我很遗憾我没有。这正是我的问题:我必须设置 imagettftext($image, 20, 20, 20, 20, $black, $font, 'TESTING TEXT'); 才能开始看到一小部分文本,然后我从那里拿走了它

标签: php gd imagettftext


【解决方案1】:

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

错误是 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);
?>

【讨论】:

  • 哇,这太愚蠢了。 Y 位置是文本的底部,而不是顶部。啊。非常感谢。
  • 糟糕,看起来 Jatt 的答案在您之前。我将把接受的答案换成他的。不过,再次感谢。
  • 好的,我无法正确阅读。将答案切换回您的答案。嘘。
  • 真诚地你让我笑了,让我免于同样的头痛哈哈!
  • 这救了我!!谢谢!
【解决方案2】:
$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);   

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

【讨论】:

  • 是的,就是这样。讨厌的 Y 坐标是文本的底部,而不是顶部,所以我在图形顶部显示文本。嘘。
  • 我接受了马克的回答,因为它比你早了几分钟。不过非常感谢。
【解决方案3】:

会不会是你拼错了imagettftext

【讨论】:

  • imagettftext() 和 imagefttext() 是两个不同的函数。不过,感谢您提供帮助。
猜你喜欢
  • 2020-03-04
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 2020-11-10
相关资源
最近更新 更多