PNG optimisation tools

前端 未结 4 675
-上瘾入骨i
-上瘾入骨i 2020-12-08 08:53

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, w

相关标签:
4条回答
  • 2020-12-08 09:06

    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.

    0 讨论(0)
  • 2020-12-08 09:10

    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.

    0 讨论(0)
  • 2020-12-08 09:15

    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
    0 讨论(0)
  • 2020-12-08 09:20

    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).

    0 讨论(0)
提交回复
热议问题