Determine jpg compression speed?

Usually, when an image appears on my site, I save it as jpg using the image library that I wrote with a default quality of 80%. Now, when I need to perform some other operation (for example, crop it or even just resize it), the image will be opened as jpg, processed, and then saved back. However, if it was compressed before I want to compress it again, or every time I need to perform an operation, the quality will drop.

Is there a way to determine how much the image is already compressed before (compared to the version of png that I assume) using the tools in the standard GD php libraries? I know that tools that detect where the image was shot using Photoshop do this by comparing the relative amounts of compression, so I think you can determine the amount of compression, but does anyone know how I will do this calculation? Thanks.

+6
source share
3 answers

You cannot get quality value from JPG. In addition, the quality value depends on the encoder. This is not a standard or anything like that. Some programs have only (low, medium, high), about 20 may be better than 90.

Secondly, JPG will simply lose quality in every indirect coding, even if you save it as the best quality every time. The sad truth of life :) The only operations that are not worse quality are shifts and rotations (and yields, if they are aligned to the size of the JPEG block).

Rule of thumb: Encode it every time with the same quality value. Example. If you save it once, say 60, then there is no win, if you save it 80 times the next time, Just a large file size.

In addition, try to reduce the number of such re-encodings and perform each manipulation on the original, if you have enough free space.

+8
source

You will need to save the image quality in the database so that you can know if it was already compressed or not.

+1
source

To avoid compressing the image several times, you can simply compare the resized file size with the original.

  • If the file size is noticeably larger, you have increased speed, so do not use it.
  • If it is noticeably lower than compressed, use it.

In addition, when you recompress the file at the same speed, it only saves a small amount from the file size, if you use round numbers for your bids (60%, 70%, 80%, etc.), you can set the speed if the size file size is very similar to the original.

For example, a file compression of 1,844 KB at:
- 90% = 2,115 KB . The size has increased, so I will not use it.
- 80% = 1,843 KB . This is almost identical to the original file size, so I can assume that the original has a speed of 80% .
- 70% = 1,567 KB . It has shrunk, so I will use it.

Finally, if you are only interested in image compression speeds, you can use PHP to save the speed that you use in the file metadata.

+1
source

Source: https://habr.com/ru/post/908745/


All Articles