19

I'm wondering how to figure out the best compress rate (small filesize + no quality loss) automatically.

At the moment I'm using imagejpeg() with $quality = 85 for each .jpg.

PageSpeed (Chrome Plugin) suggests, to lower the quality of a few images to save some kb. The percentage of reduction is different.

I'd like to write a cronjob that crawls a specific directory and optimizes every image.

How does PageSpeed or TinyPNG figure out the best optimized quality and is this possible with PHP or another serverside-language?

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
Mr. B.
  • 8,041
  • 14
  • 67
  • 117
  • You are already caching the `imagejpeg()` calls, right? – Pekka Oct 31 '13 at 00:12
  • @Pekka: yes, I do. I would resize the images with a quality of 100%. The cronjob would optimize it later. The 85% quality is just because I've no algorithm yet. – Mr. B. Oct 31 '13 at 00:13

1 Answers1

28

TinyPNG uses pngquant.

Pngquant has option to set desired quality, similar to JPEG. You can run something like:

<?php system('pngquant --quality=85 image.png'); ?>

Pngquant website has example code showing how to use pngquant from PHP.


For JPEG you can apply lossless jpegcrush.

JpegMini (commercial) and jpeg-archive (free) are lossy and can can automatically find a minimal good quality for a JPEG.

In PHP you can roughly estimate how much JPEG was compressed by observing how much file size changes after re-compression. File size of JPEG recompressed at same or higher quality will not change much (but will lose visual quality).

If you recompress JPEG and see file size halved, then keep the recompressed version. If you see only 10-20% drop in file size, then keep the original.

If you're compressing yourself, use MozJPEG (here's an online version).

Kornel
  • 97,764
  • 37
  • 219
  • 309
  • i've tried jpegcrush but it seems not effective. this perl script helps me reduce only 10 percent of file size. Jpegmini and tinyjpg can do up to 70%. Any free better script can do it? – TomSawyer Mar 21 '16 at 11:11
  • @TomSawyer jpegcrush is lossless and gets best filesize/quality ratio. The other ones are not, and are equivalent to re-saving the file at lower quality, adding more distortions and having potentially lower filesize/quality ratio. If a tool saves you 70% on your JPEG, it means you've created the file with wastefully high quality setting. – Kornel Mar 21 '16 at 11:21
  • I don't think so. I've tried to compress many files by jpgmini and tinyjpg and almost of them will be compressed at lease 50% and the quality is the same or you can tell what different by your human eyes. Two above services reduces my file 70% size but jpegcrush can do up to 10%. You should try two above services to see the difference – TomSawyer Mar 21 '16 at 14:30
  • 1
    @TomSawyer when I say lossless/lossy I mean it context of compression terminology where "lossless" means 100% pixels exactly bit-wise identical, not "looks same to me". Losing details that you can't see with a naked eye is good, but it is called lossy for a reason. – Kornel Mar 22 '16 at 18:37