2016-02-13 45 views
1

我输入此代码以显示文本转换为图像。imagettftext()函数不工作?

<?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.ttf"; 

// 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); 
?> 

并在此之后的输出是::

Output in mozilla

我在Windows中使用WAMP还安装了GD ...我gd_info():

array (size=12) 
'GD Version' => string 'bundled (2.1.0 compatible)' (length=26) 
'FreeType Support' => boolean true 
'FreeType Linkage' => string 'with freetype' (length=13) 
'T1Lib Support' => boolean false 
'GIF Read Support' => boolean true 
'GIF Create Support' => boolean true 
'JPEG Support' => boolean true 
'PNG Support' => boolean true 
'WBMP Support' => boolean true 
'XPM Support' => boolean true 
'XBM Support' => boolean true 
'JIS-mapped Japanese Font Support' => boolean false 

所以最后我必须做...请帮助我......

+0

你有没有试过在编辑器中查看文件? – Zulan

回答

0

试试我的代码......创建一个PHP文件 - >把这段代码,只是运行你 得到了肯定的结果...快乐作弄....

不要忘了加“arial.ttf”文件,你在哪里运行你的php文件相同的目录...

<?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.ttf'; 

     // 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); 
?> 
+0

感谢它的作品....你会解释关于错误plz ........ –

+0

我认为...那里你忘了pur arial.ttf文件 –

+0

它看起来类似于原始代码。现在怎么运作?谨慎解释? OP表示arial.ttf已经存在于脚本所在的目录中。 –

0

如果它在你后面工作对两个调用imagettftext()的注释,确保文件arial.ttf存在于脚本的相同目录中。另外注释掉对header()的调用,以便能够查看返回的任何错误消息。

+0

yah ....字体文件在同一目录中 –

+0

尝试将'arial.ttf'更改为'arial' –

+0

从文档http://php.net/manual/en/function.imagettftext.php“根据哪个版本的PHP库正在使用的GD库,当fontfile不以前导符/ /开头时,则.ttf将被附加到文件名,并且库将尝试沿着库定义的字体路径搜索该文件名。“ –