I’ve successfully implemented some PHP to download an image:
PHP:
<?php
$image = "path/to/image.jpg"
header('Content-disposition: attachment; filename=image.jpg');
header('Content-type: image/jpeg');
readfile($image);
?>
Here is an example image that I’ve saved to my server: http://www.thenally.com/work/php-tests/header/files/herzog.jpg
If I view the image in the browser, then right-click to save, it acts as a typical image on my local Windows box (ie. viewable in Windows Photo Viewer). When I download the image using the PHP script above, the image can’t be viewed in the same way, although the filesize is accurate and I can open it in Photoshop. I don’t know much about image encoding/decoding etc but obviously the file changes when I download using the method above.
EDIT:
The Windows Photo Viewer error is as follows:
“Windows Photo Viewer can’t open this picture because either Photo Viewer doesn’t support this file format, or you don’t have the latest updates to Photo Viewer”.
There are no PHP errors in my log file, but this came up in my Chrome console:
“Resource interpreted as Document but transferred with MIME type image/jpeg: “http://thenally.com/work/php-tests/header/index.php“.
2
Answers
try this
header('Content-Transfer-Encoding: binary');
I ran a similar issue and managed to fix it by removing any blank space/new lines between PHP tags.
Basically had to do this:
Just make sure you do not have any blank spaces before any PHP tags and you should be good!