【问题标题】:returning PNG image from php script recognized as document从识别为文档的php脚本返回PNG图像
【发布时间】:2015-07-30 21:28:25
【问题描述】:

如何从 php 文件返回图像?我有这个代码的image.php

<?php
$im = imagecreatefrompng("myImage.png");
header("Content-Type: image/png");
?>

但实际上输入domain.com/image.php是被浏览器识别为document,而不是图像...返回一个空结果

【问题讨论】:

  • 启用 error_reporting,您的代码不会返回任何数据(imagecreatefrom.. 单独只是实例化一个 GD 资源)。

标签: php image content-type


【解决方案1】:

好吧,你必须输出图像,确保先发送标题:

header("Content-type: image/png");
$im = imagecreatefrompng("myImage.png");
imagepng($im);

myImage.png 需要与image.php 在同一目录下,否则您需要指定图片的路径。

但如果你除了输出什么都不做,那就:

header("Content-type: image/png");
readfile("myImage.png");

【讨论】:

  • 按照@mario 的建议启用错误报告? “不工作”的描述性不是很好。
  • 啊,也许看看内容处置? header("Content-disposition: attachment; filename= "myfile.png");
猜你喜欢
  • 2014-01-17
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 2010-11-30
  • 2011-10-21
  • 1970-01-01
相关资源
最近更新 更多