2014-12-27 61 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