通过PHP编辑时的黑色图像

时间:2017-11-14 17:15:56

标签: php

我发现了几个用PHP编辑图像的教程。然而,它显示黑色图像。我无法理解这里的问题是什么。

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

  // Create Image From Existing File
  $jpg_image = imagecreatefromjpeg('http://iamsaurav.xyz/images/avatar.jpg');

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

  // Set Path to Font File
  $font_path = 'http://iamsaurav.xyz/images/oswald.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);
?>

1 个答案:

答案 0 :(得分:0)

如果您使用图像和字体的完整路径,它看起来工作正常〜显然这里使用的路径与我的系统相关,因此您需要进行适当的编辑。最可能的问题是PHP正在寻找图像的错误目录....如果文本没有出现,那么该路径也很可能是错误的。

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

  /* use absolute path */
  $jpg_image = imagecreatefromjpeg('C:/Temp2/src.jpg');

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

  // Set Path to Font File
  $font_path = 'c:/wwwroot/inc/fonts/AMAZON.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);
?>