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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog