简体   繁体   English

PHP imagejpeg 不在浏览器上显示图像

[英]PHP imagejpeg not displaying image on Browser

I am using imagejpg() to display an image on the browser and place text over the image.我正在使用 imagejpg() 在浏览器上显示图像并将文本放在图像上。

I have used this before and it has worked.我以前用过这个,它奏效了。 Now I switched domain hosts and perhaps the PHP version is different as the image is now not displaying.现在我切换了域名主机,可能是 PHP 版本不同,因为现在没有显示图像。

It displays a small square in the middle of the screen, like a broken link, but the image is definitely there.它在屏幕中间显示一个小方块,就像一个断开的链接,但图像肯定在那里。

Could someone please assist with this?有人可以帮忙吗?

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

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

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

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

$text1 = "hello";
$text2 = "there";
$text3 = "world";

//Floor 17
  imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
  imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
  imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);

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

  // Clear Memory
  imagedestroy($jpg_image);

Thank you in advance.先感谢您。

Rob

I just figured it out.我只是想通了。 Answer below下面回答

I moved the header down and it sorted out the problem.我将标题向下移动并解决了问题。

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

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

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

$text1 = "hello";
$text2 = "there";
$text3 = "world";

//Floor 17
  imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
  imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
  imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);

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

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

  // Clear Memory
  imagedestroy($jpg_image);

I had the same problem, found out that if there is a blank space before the header, the image doesn't work.我遇到了同样的问题,发现如果标题前有空格,则图像不起作用。 I was able to save the image on the server but not to get the browser to display it.我能够将图像保存在服务器上,但无法让浏览器显示它。 Removing every blank space did the trick for me.删除每一个空格对我来说都是成功的。 I think it's the same reason why your script didn't work with the header at the beginning.我认为这与您的脚本在开始时无法与标题一起使用的原因相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM