PDA

View Full Version : Jpeg finding the image quality in PHP



Morgenmuffel
10-04-2010, 08:42 AM
Hi all

I have been having an issue with php and image resizing/resampling,

Basically i have an upload function that resizes and resamples jpegs and it works fine, however there is one small issue,

What i want to do, is to only reduce the jpeg quality on all images that have a jpeg quality of above 80%, any jpegs that have a quality of lower than 80% should be ignored by the resample function.

However i can't figure out how to detect the jpeg quality, so currently all images are resampled which results in some images becoming larger than the initial image

So basically - How do i detect jpeg quality in PHP

Thanks

fred_fish
10-04-2010, 09:32 AM
AFAIK there is no "quality" for an existing jpeg, it is only a parameter passed to the compression algorithm that tells it how much data to chuck out.
Unless the compressor writes it in the EXIF header, but I haven't seen that.
Maybe you could compare the filesize with the resolution and make a call based on that?

Erayd
10-04-2010, 12:00 PM
^ What he said - it's a parameter passed to the encoder, it's not something you can fish out of an existing image.

Morgenmuffel
10-04-2010, 02:47 PM
Then how do programs like fireworks etc know what level of compression an image has, when i open an existing image?

I am not doubting you guys, i just don't understand, does fireworks estimate it?

Erayd
10-04-2010, 03:13 PM
Then how do programs like fireworks etc know what level of compression an image has, when i open an existing image?I'm guessing so. However... if you have images that Fireworks is correctly figuring out the compression quality for, try taking a look at the EXIF data attached to the image - it may be that whatever compressed it has left a note in there.

Morgenmuffel
10-04-2010, 05:37 PM
I don't suppose there is away to guess in php

Alternatively I could just resample all images, then compare the size of input image against the output image and keep the smaller one

Erayd
10-04-2010, 10:07 PM
I don't suppose there is away to guess in php...

Well as fred_fish suggested, why don't you compare the resolution with the filesize? That'd certainly give you a decent starting point for a guess.

Morgenmuffel
10-04-2010, 11:09 PM
No worries, I just have a person who regularly puts up 1mb images and then sets them to display at thumbnail size in their pages, and can't understand why their pages are slow and the server space is shrinking, thought I would idiot proof the upload part of FCKeditor

Erayd
10-04-2010, 11:50 PM
Aaah, one of *those* users. Smart thinking :thumbs:.