2014-12-27 62 views
0

图像加载到浏览器很好,但是当我尝试写入一些文本,它的图像中断(像这样:http://www.tradenepal.com.np/test.php)。当我注释掉imagettftext()时,图像不会再次加载。这发生在我的本地主机上,我使用WampServer版本2.5。我已经对inetrnet发表过如此多的评论,但我似乎无法知道问题所在。任何帮助将不胜感激。谢谢。我的代码:PHP imagettftext()打破了图像,如果我使用imagecreatefrompng,imagecreatefromjpeg和imagecreatefromgif

<?php 

//Set content type 
header('Content-type: image/jpeg'); 

// Create image from existing image 
$jpgImage = imagecreatefromjpeg('file.jpg'); 

// Allocate color for text 
$white = imagecolorallocate($jpgImage, 255, 255, 255); 

// Set Path to Font File 
$font = 'arialbd.ttf'; 

// Text to print to image 
$text = 'Testing text output'; 

// Print Text On Image 
imagettftext($jpgImage, 75, 0, 50, 400, $white, $font, $text); 



// Send Image to Browser 
imagejpeg($jpgImage); 

// Clear Memory 
imagedestroy($jpgImage); 

?> 
+0

首先检查图像是否足够大,以便将测试放置在已定位的位置。其次检查'arialbd.ttf'文件是否正确地位于您指定的位置。 – RiggsFolly 2015-01-01 15:19:10

+0

谢谢RiggsFolly,在提问后我想到了。 – HazardPJ 2015-01-08 05:22:47

回答

0

//发送图片到浏览器

imagepng($jpg_image); <------ remove image type png 

//输出的图像

imagejpeg($jpg_image); 

我测试过,它的工作原理。

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

    // Create Image From Existing File 
    $jpg_image = imagecreatefromjpeg('file.jpg'); 

    // Allocate A Color For The Text 
    $white = imagecolorallocate($jpg_image, 255, 255, 255); 

    // Set Path to Font File 
    $font_path = 'arialbd.ttf'; 

    // Set Text to Be Printed On Image 
    $text = "This is a sunset!"; 

    // Print Text On Image 
    imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text); 

    // Send Image to Browser 
    imagejpeg($jpg_image); 

    // Clear Memory 
    imagedestroy($jpg_image); 
?> 
+0

感谢您的回复BuseGönen。我纠正了你指出的错误,但我也意识到了其他错误,所以我编辑了它,但它仍然无法正常工作。你能再次检查我吗?谢谢。 – HazardPJ 2014-12-27 19:38:50

+0

我的GD库工作正常 – HazardPJ 2014-12-27 19:40:30

+0

我的代码,我测试过了。工作正常。没问题。您的更改图像。 – 2014-12-27 19:47:23