使用 PHP 标头下载图像:图像未显示在 Windows 照片查看器和 MacOS 预览中


Image download with PHP header: Images not displaying in Windows Photo Viewer and MacOS Preview

我在使用 PHP 标头函数下载图像时遇到问题:图像正在正确下载,可以在 Photoshop 中查看,但无法在 Windows 照片查看器和 MacOS 预览中打开。这是我使用的(简单)代码:

header('Content-type: image/jpg');
header('Content-Disposition: attachment; filename="'.$url.'"');
readfile($image);

非常感谢

亚历克斯

我找到了解决方案(可能对其他人感兴趣):在执行 PHP 标头代码之前,不要执行任何 HTML/JavaScript 代码。否则,HTML/Javascript代码写在图像文件中,因此无法在Windows照片查看器等中显示。Photoshop似乎以某种方式过滤掉了它。