dougaxing8673 2018-09-19 07:27
浏览 477
已采纳

PHP:imagepng和imagejpeg输出无效16x16图像[关闭]

Problem on a remote server (while the same php script works on a local) is:

1) PNG image is loaded from a system path:

    $imgpath = "absolute/path/to/a/png/image";
    $img = imagecreatefrompng($imgpath);

File and folder where file is stored has proper chmod and chown setup and I can output correct image size (for test):

    echo imagesx($img)." x ".imagesy($img);

Above line outputs 256 x 64 which is expected image size. I assume this means that $img object is properly loaded from the file.

2) When I try to output the image using:

    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);

...browser shows an empty image of 16 x 16 pixel size.

Screenshot of how it looks like

3) If I try to open the same file using direct http url, browser opens it correctly (expected 256 x 64 image with some colorful content).

Here is how image looks like when opened with direct link

4) If I remove header('Content-type...., imagepng outputs png file content as expected:

�PNG IHDR@z?>� pHYs���+�IDATx��]ytչ����͒-�Y����$�� ��f!$� Jh M)-�+=��q�Bii�e����ihi d!!!�B�=^B�D��E��}��y���I�H��s��䜌>ݙ{�߽�~����ct�e���]���~....

(showing only part of the output here)

5) When I compare the output to what I can see on remote server using nano, seems 100% match in bytes and in file size. Also file size is exactly the same if I download the image using direct URL in browser.

6) I've also tried to output image with different methods, such as fpassthru and echo file_get_contents(... and setting up header for that purpose (adding content size etc.). All resulted the same.

I'm going nuts here. Anyone has any idea what could be the problem?

P.S. request status is 200, no errors and no server errors in log file.

P.P.S. I have compared request and response headers when I open image with direct link and via PHP script. They are exactly the same.

  • 写回答

1条回答 默认 最新

  • douqin2108 2018-09-19 08:33
    关注

    The only way I can reproduce this issue is when I output content before changing the header. Can you please check if there are no spaces before <?php tag and no other possible outputs

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?