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?
file_get_contents()
you don't need the output buffer and there are alternatives togzencode()
) but I'm not sure it would make a difference. Have you tried not-gzipping it? Are these tools intended to test this type of response? Have you tried other real browsers (apart from Opera 11.6)? And looked at the headers/response in these browsers? "Viewport" - you are forcing a download, so what does the viewport have to do with it? "SEO friendly" - you are forcing a download, SEO would seem to be irrelevant here? Isopenh264.org
your domain?file_get_contents()
uses no compression, as I want the download to offer the highest quality image possible regardless of speed. I have tried not gzipping it and the results are much worse and I posted those results somewhere else on this website. openh264.org is not my domain at all. This is also part of a site I'm monetizing with adsense and I believe this affects my income because the robots believe the current image download idea causes the whole user experience to be slower for the reasons mentioned above.