Avatar of mike99c
mike99c

asked on 

Advice on free image compression for PHP

I would like advice on a free image compression script I can use in PHP. This script needs to at least compress JPEG and PNG images but WebP is optional. I would also like the compression to not show any visible loss in quality.
Images and PhotosPHP

Avatar of undefined
Last Comment
David Favor
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

1. Compression is either lossy (e.g. JPEG) or lossless (e.g. PNG).  Anytime you save a JPEG, you lose quality - it is reprocessing the image. There is a quality level (between 0 and 100, with 100 being the highest quality) when saving JPEGs, but the higher the quality, the larger the file size. Usually you can compress a JPEG to a quality level of about 70 or so without any extreme loss in visual quality (if you look closely you can see the compression artifacts), but every time you re-save the same image, you lose quality (if you saved the same JPEG a hundred times at quality 90, you could clearly see the loss).

2.With PNG, there is little to no room in terms of quality levels or anything like that. Usually you have 24-bit PNGs or 8-bit PNGs, where 24-bit PNGs have support for millions of colors while 8-bit PNGs have support for 256 colors. But if you open up an 8-bit PNG and re-save it as an 8-bit PNG, for example, there is no compression. If you open up a 24-bit PNG and save it as an 8-bit PNG, you'll get a lot smaller file size, but if the image had millions of colors, then you'll definitely notice quality loss because you're throwing out millions of colors and trying to force the picture into a much smaller palette of possible colors.

3. So if your source files are already JPEG and PNG, then there's probably not much you can do about them. Every once in a while, you might come across a 24-bit PNG that you could save as a high-quality JPEG and it would mostly look the same and have a smaller file size, but that's a rare case.

4. If your source files are things like BMPs (uncompressed bitmaps), then you can save a BMP to either JPEG or PNG and see a dramatic reduction in file size, because the source file isn't optimized in any way - it's just raw data.

5. If you want to RESIZE a very large image (e.g. you get a HUGE JPEG and you only need it at a smaller resolution), then that would reduce file size.

6. That said, your most simple option is just to use the command line utility ImageMagick, which can convert between different formats. And you can use PHP's shell_exec() (or similar) function to call it. If you can't use ImageMagick, then search for PHP scripts using the GD extension. Just remember, you can't very effectively optimize / compress an image that is already optimized / compressed.

If you're using a CMS like WordPress or something, there are a dozen different free plugins that will try to optimize your uploaded images for you.
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of David Favor
David Favor
Flag of United States of America image

Random additional mozjpeg note, for people reading this question in the future...

The mozjpeg tool is one of the craftiest compression tools around.

I'm getting 90%+ file reduction size before human perception registers image quality reduction.

Anytime you're fixing SEO problems, say suggestions from Google Insights or GTMetrix or WebPageTest, start with mozjpeg for your JPEG images + you'll be surprised how well this tool works.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo