Motive
Motive

Reputation: 3111

How to get quality of image in PHP

I've been doing some speed optimization on my site using Page Speed and it gives recommendations like so:

Optimizing the following images could reduce their size by 35.3KiB (21% reduction).

Losslessly compressing http://example.com/.../some_image.jpg could save 25.3KiB (55% reduction).

How are the calculations done to get the file size reduction numbers? How can I determine if an image is optimized in PHP?

As I understand it, they seem to base this on the image quality (so saving at 60% in Photoshop or so is considered optimized?).

What I would like to do is once an image is uploaded, check if the image is fully optimized, if not, then optimize it using a PHP image library such as GD or ImageMagick. If I'm right about the number being based on quality, then I will just reduce the quality as needed.

How can I determine if an image is fully optimized in the standards that Page Speed uses?

Upvotes: 3

Views: 1906

Answers (2)

ernie
ernie

Reputation: 6356

It looks like you could use the PageSpeed Insights API to get the compressions scores: https://developers.google.com/speed/docs/insights/v1/reference, though I'm guessing you'd want to run a quality check/compression locally, rather than sending everything through this API.

It also looks like they've got a standalone image optimizer available at http://code.google.com/p/page-speed/wiki/DownloadPageSpeed?tm=2, though this appears to be a Windows binary. They've got an SDK there as well, though I haven't looked into what it entails, or how easy it would be to integrate into an existing site.

Upvotes: 0

Michael Ryan
Michael Ryan

Reputation: 53

Chances are they are simply using a standard compression or working on some very simple rules to calculate image compression/quality. It isn't exactly what you were after but what I often use on uploaded images etc etc dynamic content is a class called SimpleImage:

http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

this will give you the options to resize & adjust compression, and I think even change image type (by which I mean .jpg to .png or .gif anything you like). I worked in seo and page optimization was a huge part of my Job I generally tried to make the images the size the needed to be no smaller or bigger. compress JS & CSS and that's really all most people need to worry about.

Upvotes: 2

Related Questions