views:

387

answers:

4

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.

A: 

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.

Mike
Thanks, but I'm after something that goes further than just gzipping, ie removing colour correction and gamma information.
MatW
Although I agree there are other optimization techniques besides simple compression, doing what you are asking is not _optimizing_, but rather _destroying_ the uploaded image.
Mike
Bah! Semantics! :) As a point of correctness though: What I'm asking isn't destroying the image in any way, rather the image is being optimised by destroying certain pieces of (unnecessary) info.
MatW
All web browsers support color profile and gamma correction now. If someone uploads an image to Flickr, and it looks correct on every web browser, then they upload it to your web site and looks all washed out because you stripped the color profile out, guess who they will blame? Not trying to be pedantic or righteous, just not sure if you were aware of this.
Mike
No worries; good point. At the moment I'm taking the gamble / making the judgement call that there won't be significant gamma / colour profile variations in my user's uploaded photos.
MatW
+2  A: 

Have you heard of PNGCrush? You could check out the source, part of PNG and MNG Tools at SourceForge, and transcribe or wrap it in Python.

Kache4
PNGcrush is faster than any pure Python solution will ever be, and calling external programs is easy in Python. Most obvious downside is if your OS has an extremely high cost for spawning a new process, e.g. Windows.
Theran
I somehow doubt that spawning a new process for every file upload is a good idea regardless of your OS.
Eric
A: 

I would question the wisdom of throwing away other chunks (like gAMA and iCCP), but if that's what you want to do it's fairly easy to use PyPNG to remove chunks:

#!/usr/bin/env python
import png
import sys

input=sys.stdin
out=sys.stdout

def critical_chunks(chunks):
    for type,data in chunks:
        if type[0].isupper():
            yield type,data

chunks = png.Reader(file=input).chunks()
png.write_chunks(out, critical_chunks(chunks))

the critical_chunks function is essentially filtering out all but the critical PNG chunks (the 4 letter type for a critical chunk starts with an uppercase letter).

David Jones
This doesn't changed the zlib compression, as it passes the chunks through untouched.
David Jones
A: 

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
Burntime