【问题标题】:Joomla 1.5. Text over image doesn't workJoomla 1.5。图片上的文字不起作用
【发布时间】:2013-03-03 10:58:34
【问题描述】:

我正在使用 Joomla 1.5。我有创建带有文本的图像的脚本,但它对我不起作用:

<?php
// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = "ARIAL.TFF";

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

我不明白为什么这不起作用。我在谷歌找到了很多例子,我用过它,但总是一样的。 这个错误的英文意思是:Image "http://juokoera.lt/a.php" can't be shown, because It have problems (errors).

我在 google 上发现,这可能是我的主机故障,我更改了它,但同样的问题。如果可以,请帮助我。非常感谢。

更新: 我遇到了同样的错误,代码如下:

dasfasdf
dfas

<?php 
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = 'Testing...';
$font = "ARIAL.TTF";
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im); ?>

如何在同一个 php 中使用额外的文本?

【问题讨论】:

  • 我猜你没有可用的“ARIAL.TFF”。确保它确实存在。当然,在注释该行之后,您的代码会生成没有任何文本的图像。
  • 另外,一个常见的错误是在代码之前有任何输出。 IE。即使是图像生成 PHP 代码之前的换行符或空格字符也会导致此错误。
  • @varnie 缺少的字体文件引用不应产生此错误,只需将图像留空,不带文本。
  • ARIAL.TFF 被放置在 .../public_html/ 文件夹中,也许我需要像$font = "/public_html/ARIAL.TFF"; 那样写到 ttf 的路径?这就是我所有的 a.php 文件,里面没有更多内容。
  • 我用另外 1 个代码更新了我的帖子,这些代码对我返回了相同的错误。

标签: php image text joomla imagettftext


【解决方案1】:

我认为你的问题在这里:

$font = "ARIAL.TFF";

事实上,在您的服务器上不存在名为“ARIAL.TFF”的文件,您要查找的字体扩展名是 TTF,而不是 TFF,实际上您的服务器上存在“ARIAL.TTF”,我只是下载它,将该行更正为:

$font = "ARIAL.TTF";

之后,您应该可以在图像上写文字了

希望对你有帮助

更新

问题更新后,我注意到打印出一些文本后发送了一个标题。

header() 函数之前不需要打印任何内容,以便正确发送标头。

PHP manual>header() 中首先说明的是:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

【讨论】:

  • 天啊,愚蠢的错误......我一直认为这是 TFF 而不是 TTF......非常感谢,这解决了我的问题。
  • 我不明白为什么我的代码在使用相同的代码时不起作用在它前面添加一些文本?
  • @user2055113 现在我看到您的 a.php 运行良好...问题出在哪里?
  • 这里,看看这个juokoera.lt/1b.php
  • 我刚看到更新,当你发送一个header时,nothing必须在header之前打印出来。来自PHP manual>header()Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
猜你喜欢
  • 2011-04-21
  • 2014-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
相关资源
最近更新 更多