Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect JPG Compression Rate?

Normally when an image comes into my site I save it as jpg using an image library I wrote with the default quality of 80%. Now when I need to do some other operation on it (like cropping it, or even just resizing it) the image will be opened as jpg, processed, then saved back. However, if it has been compressed before I don't want to compress it again or else every time I need to do an operation the quality will fall.

Is there a way that I can detect how much the image has already been compressed before (in comparison to a png version of it I guess) using tools in the standard GD php libraries? I know that tools which detect where an image has been Photoshopped do so by comparing relative amounts of compression so I would think it is possible to detect the amount of compression but does anyone know how I would go about doing this calculation? Thanks.

like image 327
hackartist Avatar asked Feb 17 '12 21:02

hackartist


People also ask

How do I find the compression ratio of a JPEG?

Right-click the image name and select "View layer metadata" in the drop-down. Click on Image properties to view the compression ratio.

How do you check the quality of a JPEG?

To check a photo's resolution on a Windows PC, select the file you want to use. Right-click on the image and then select "Properties." A window will appear with the image's details. Go to the "Details" tab to see the image's dimensions and resolution.

How do you know if a photo is compressed?

You can pretty much determine if the file is compressed by looking at the file type. If you inspect the string toDataURL() produces, you will see a mime-type defining either a PNG or JPEG file - in some cases where browsers support other file formats you can also see BMP and ICO file formats.

What is the best compression ratio for JPEG images?

The best compression ratio to retain image quality is 10:1. If you're looking to reduce the file size of your photographs while keeping image quality, this is the maximum compression ratio you want to shoot for.


3 Answers

You cannot get quality value from JPG. Moreover, the quality value is encoder dependent. It is not a standard or anything like this. Some programs have only (low, medium, high), in some 20 might be better than 90.

Secondly, JPG simply will lose quality in each cosnequent encoding, even if you save it as best quality every time. Sad truth of life :) The only operations that do not worse the quality are flips and rotations (and crops, if they are aligned to JPEG block size).

Rule of the thumb: Encode it every time at the same quality value. Example: If you save it once at let's say 60, then there is no gain if you save it at 80 next time. Just bigger file size.

Also, try to reduce the number of such re-encodings and perform each manipulation on the original , if you have enough storage available.

like image 79
Rok Kralj Avatar answered Nov 04 '22 18:11

Rok Kralj


You will have to store the image's quality in a database so you can know if it has already been compressed or not.

like image 23
ppp Avatar answered Nov 04 '22 16:11

ppp


To avoid compressing the image multiple times you could simply compare the file size of the resize with the original.

  • If the file size is noticeably larger then you've increased the rate, so don't use it.
  • If it's noticeably lower than it has compressed, so use it.

Also, as recompressing a file with the same rate only shaves a small amount off of the file size, if you use round numbers for your rates (60%, 70%, 80% etc.) you could ascertain the rate if the resize's file size is very similar to the original's.

For example, compressing a 1,844 KB file at:
- 90% = 2,115 KB. The size increased, so I won't use it.
- 80% = 1,843 KB. This is almost identical to the original's file size so I can assume that the original has an 80% rate.
- 70% = 1,567 KB. This has compressed, so I'll use it.

Finally, if you're only concerned with the rates of images that you've compressed yourself, then you could use PHP to save the rate you use in the file's metadata.

like image 37
nick Avatar answered Nov 04 '22 16:11

nick