This is my php code.
<?php ob_start(); echo file_get_contents("originalhighqualityphoto.jpg"); $dat=ob_get_contents(); ob_end_clean(); $dat=gzencode($dat,2); header("content-disposition: attachment; filename=\"OptimizedPhoto.jpg\"",true); header("content-encoding: gzip",true); header("content-type: image/jpeg",true); header("cache-control: max-age=1000000",true); header("content-length: ".strlen($dat),true); echo $dat;exit(); ?>
What I'm trying to do is make it so that the images will download at 100% quality and faster off my website without having to offer them as zip files. I don't want to use direct image compression (like with photoshop for example) because thats already used elsewhere on my site as "previews" to the high quality images.
I tested the download in my own web browser (Opera v11.6) and it works normally.
Here is what throws me off. I used webpagetest.org to try to download the images and the weirdest results are as follows:
Via firefox at webpagetest.org:
Leverage browser caching of static assets: 75/100 WARNING - (1.8 days) - http://ciscobinary.openh264.org/openh264-win32-v1.3.zip
Note: I did not name any file as a zip file and when I manually accessed that file, it contained two random files, one being a dll file.
Test via IE8 at webpagetest.org:
Compress Images: 62/100 8.0 KB total in images, target size = 5.0 KB - potential savings = 3.0 KB FAILED - (8.0 KB, compressed = 5.0 KB - savings of 3.0 KB) - http://example.com/path/to/testphpfile.php
I don't understand where the 8.0 KB comes from when the image I used in the test is over 100KB.
In chrome via webpagetest.org the test seems OK.
In page speed insights, the score is OK but the output is garbled.
Properly formatting and compressing images can save many bytes of data. Optimize the following images to reduce their size by 36.8KiB (20% reduction). Losslessly compressing http://example.com/path/to/testphpfile.php could save 36.8KiB (20% reduction).
Also, in page speed insights when tested via mobile, it goes on about configuring a viewport, but I don't think that's possible with an HTTP header only.
speaking of which.... Are the headers causing these results or is there something else I need to do out there to make the image downloading script work and seo friendly?