0

我正在使用 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).

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

更新: 我得到了同样的错误,代码看起来像:

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 中使用其他文本?

4

1 回答 1

0

我认为你的问题在这里:

$font = "ARIAL.TFF";

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

$font = "ARIAL.TTF";

之后,您应该能够在图像上写文字

希望这可以帮助

更新

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

header()为了正确发送标头,在函数之前无需打印任何内容。

PHP 手册>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.

于 2013-03-03T11:34:17.397 回答