如果我使用imagecreatefrompng,imagecreatefromjpeg和imagecreatefromgif,PHP imagettftext()会破坏图像

时间:2014-12-27 13:55:34

标签: php image gd wampserver imagettftext

图像被加载到浏览器中,但是当我尝试在其上写入一些文本时,图像会中断(如下所示:http://www.tradenepal.com.np/test.php)。当我注释掉imagettftext()时,图像不会再次加载。这发生在我的localhost上,我使用的是WampServer版本2.5。我对inetrnet做了很多评论,但我似乎无法知道问题是什么。任何帮助将不胜感激。谢谢。我的代码:

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

?> 

1 个答案:

答案 0 :(得分: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);
?>