Apache/PHP Image Compression

Does anyone know of a good option for compressing images via PHP? I’m about to test with the latest version of Imagick, but am looking around for other options. Currently migrating our site over to a better server setup and so now is the best time to investigate such solutions.
Basically we have a photography department here for taking photos of each product that comes in before being listed online (this is for an ecommerce site). I have them running the images through batch scripts to properly compress them via “save for web” and then they upload the images from there. While this is fine, we have issues where they will temporarily upload images from the manufacturers website before replacing it with ones we took, but these images are not always optimized properly. I’ve seen occasions where the thumbnail size for a 100x100 image was 18 KB! Now, this is with a super old version of Imagick, i’m hoping the newest version is better at compressing. But, while i’m testing that i’m open to other suggestions while i’m at it.
Thanks!

We use a two step process.

  1. We use ImageMagick – Convert, Edit, or Compose Digital Images with 85% compression rate.
  2. we apply a lossless compressioin that strip all meta data using jpegtran.exe
    In jpegtran, use the -copy none -optimized for images < 10k and -copy none -progressive on image > 10k

If you don’t mind doing a little manual work, you can use http://www.jpegmini.com. It does a really good job compressing jpg and then running #2 on top.

If you want to optimize even further, than you can use webP compression for images. It’s native for chrome users, but people using IE of FF will have to install the “codec”. All the info can be found here: An image format for the Web  |  WebP  |  Google Developers

Hope this helps

Solution from the Yahoo Performance Team Best Practices on how to optimize images:
[list]
[] You can check the GIFs and see if they are using a palette size corresponding to the number of colors in the image. Using imagemagick it’s easy to check using
identify -verbose image.gif
When you see an image useing 4 colors and a 256 color “slots” in the palette, there is room for improvement.
[
] Try converting GIFs to PNGs and see if there is a saving. More often than not, there is. Developers often hesitate to use PNGs due to the limited support in browsers, but this is now a thing of the past. The only real problem is alpha-transparency in true color PNGs, but then again, GIFs are not true color and don’t support variable transparency either. So anything a GIF can do, a palette PNG (PNG8) can do too (except for animations). This simple imagemagick command results in totally safe-to-use PNGs:
convert image.gif image.png
“All we are saying is: Give PiNG a Chance!”
[] Run pngcrush (or any other PNG optimizer tool) on all your PNGs. Example:
pngcrush image.png -rem alla -reduce -brute result.png
[
] Run jpegtran on all your JPEGs. This tool does lossless JPEG operations such as rotation and can also be used to optimize and remove comments and other useless information (such as EXIF information) from your images.
jpegtran -copy none -optimize -perfect src.jpg dest.jpg
[/list]

All the tools listed are available as comand line tools so you just have to install these and call them in a PHP script.

Thanks, I will check out those tools.

I’d just like to say thanks to mrPerezMarc and allogarage for their tips, I’ve been looking for a good way to compress images via PHP (toying with Imagick) on behalf of a client for half the day - finally managed to put something together everyone is happy with!

I’m debating about compressing some with webP, but until Google takes over the world I fear of alienating my FF/IE userbase…

@SpyGrey, I believe the browsers that support webP send an accept header that indicates it. You could serve webP to browsers that support it and fall back to jpeg for browsers that don’t (though you’d want to add a vary header so proxies don’t cache the wrong image type).

After I tested my website, the un-optimized images would only save a minimal amount if I compressed them, but as I have a large e-commerce website, I would like some sort of solution to automatically compress all my images.
[color=#efefef]replica watches[/color] [color=#efefef]replica watch[/color] [color=#efefef]replica watches[/color] [color=#efefef]replica watch[/color] [color=#efefef]swiss watch replicas[/color]
Also, thanks a lot mrPerezMarc, www.jpegmini.com looks absolutely awesome. I’ve used a few really good jpg optimizers online, but I’m extremely excited to see if this website can top them. edit: it kinda annoying that they dont let you pick various stages of optimization, instead giving you just one choice only.

Glad my advice helped :slight_smile:

Actually there is such a thing as animated pngs nowadays.

APNGs are not supported on most browsers, so I think it’s a bad idea to use these for now.

After reading the documentation, if a browser doesn’t doesn’t support the APNG, it will display the first frame of the image.

Of course, if your goal was to show a really cool animation, that sucks a little, but then again, do you really care about ppl who has an old browser? :stuck_out_tongue:

IE, Chrome and Safari don’t support natively APNG…

But will they show the first frame?