PHP图像无法显示

时间:2014-03-19 14:04:05

标签: php gd

我试图在php中创建一个包含一些文本的动态图像,但是它说图像因为包含错误而无法显示。这是我的代码

    <html>
<head>
    </head>
    <body>
<?php
$width = $_POST["width"];
$height = $_POST["height"];
$text = $_POST["text"];
$border = $_POST["border"];
$cornerangle = $_POST["angle"];

putenv('GDFONTPATH=' . realpath('.'));
header ('Content-Type: image/png');
$img = imagecreatetruecolor($width, $height);
$font = 'verdana';
$font_size = 20;
$angle = 45;
$text_box = imagettfbbox($font_size,$angle,$font,$text);
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[3]-$text_box[1];
//coord text
$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);
$textcolor = imagecolorallocate($img,255,255,255);
imagettftext($img, $font_size, 0, $x, $y, $textcolor, $font, $text);
imagepng($img);
imagedestroy($img);
?>
</body>
</html>

我的字体文件(verdana.ttf)与我的php文件位于同一文件夹中。我试过了$ font =&#39; verdana.ttf&#39 ;;并得到了同样的错误。

1 个答案:

答案 0 :(得分:1)

您需要在Web服务器中使用该字体并调用它。

$font = 'path/verdana.ttf';

我测试了你的代码,并在我的服务器上正常工作。