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

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

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

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题