2014-03-19 168 views
0

我试图在PHP中创建一个动态图像,其中包含一些文本,但它表示图像无法显示,因为它包含错误。这里是我的代码无法显示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> 

我有与我的php文件相同的文件夹中的字体文件(verdana.ttf)。我试过$ font ='verdana.ttf';并得到了同样的错误。

+0

你应该发布你的错误消息。 – 2014-03-19 14:09:01

+0

尝试 $ font ='。/ verdana.ttf' – Kriss

+0

错误是“图像'localhost://test.php'由于包含错误而无法显示”。我应该提到我使用的是WAMP,但我在phpinfo()中有GD和FreeType支持。使用imagestring而不是imagettftext工作,所以它必须是与imagettftext的东西。 – Adrian

回答

1

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

$font = 'path/verdana.ttf'; 

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

+0

是的,这是我的wamp问题。我在另一台服务器上测试过它,它工作正常 – Adrian