Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PNG optimisation tools

A while back I used a PNG optimisation service called (I think) "smush it". You fed it a weblink and it returned a zip of all the PNG images with their filesizes nicely, well, smushed...

I want to implement a similar optimisation feature as part of my website's image upload process; does anyone know of a pre-existing library (PHP or Python preferably) that I can tap into for this? A brief Google has pointed me towards several command line style tools, but I'd rather not go down that route if possible.

like image 609
Mathew Avatar asked Jan 03 '10 02:01

Mathew


2 Answers

Execute with PHP this command line tools

  pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -l 9 -max -reduce -m 0 -q IMAGE
  optipng -o7 -q pngout.png
  pngout pngout.png -q -y -k0 -s0
  advpng -z -4 pngout.png > /dev/null
  • pngcrush
  • OptiPNG
  • pngout
  • advpng
like image 144
Burntime Avatar answered Sep 28 '22 08:09

Burntime


As long as your PHP is compiled with GD2 support (quite common nowadays):

<?php
$image = imagecreatefromstring(file_get_contents('/path/to/image.original.png'));
imagepng($image, '/path/to/image.smushed.png', 9);

This will read in any image format GD2 understands (not just PNG) and output a PNG gzipped as the maximum compression level without sacrificing quality.

It might be of less use today than years ago though; most image editors already do this, since gzipping doesn't cost as much CPU-wise as it used to.

like image 31
Mike Avatar answered Sep 28 '22 08:09

Mike