Skip to content

Instantly share code, notes, and snippets.

@swarminglogic
Last active August 29, 2015 14:06

Revisions

  1. swarminglogic revised this gist Sep 5, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions pngopt.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/bin/bash

    # pngopt: A wrapper for pngcrush, that makes it easier to use for overriding images with its optimized output.
    # pngopt: A wrapper for pngcrush, that makes it easier to use for overriding
    # images with its optimized output.
    #
    # use: pngopt FILE1 [FILE2] [FILE3] ...
    #
    @@ -13,21 +14,20 @@
    #

    while test $# -gt 0; do
    if [ ! -f $1 ] ; then
    if [ ! -f "$1" ] ; then
    echo "Error: Bad input file: $1" >&2
    shift
    continue
    fi

    tmpfile=$(tempfile --suffix ".png")
    pngcrush "$1" $tmpfile > /dev/null
    if ls -l $1 $tmpfile | awk '{print $5}' | xargs echo | awk '{exit !($1 > $2)}' ; then
    filesizes=$(ls -l "$1" $tmpfile | awk '{print $5}' | xargs echo)
    if <<<$filesizes awk '{exit !($1 > $2)}' ; then
    echo -n "$1 "
    ls -l $1 $tmpfile | awk '{print $5}' | xargs echo | \
    awk '{print "("(100 * ($1/$2 - 1))" %)"}'
    <<<$filesizes awk '{printf "(%.2f", (100 * ($1/$2 - 1)) ; print " %)"}'
    cp $tmpfile "$1"
    rm $tmpfile
    fi

    shift
    done
  2. swarminglogic revised this gist Sep 5, 2014. 1 changed file with 0 additions and 0 deletions.
    Empty file modified pngopt.sh
    100644 → 100755
    Empty file.
  3. swarminglogic renamed this gist Sep 5, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. swarminglogic created this gist Sep 5, 2014.
    33 changes: 33 additions & 0 deletions pngopt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #!/bin/bash

    # pngopt: A wrapper for pngcrush, that makes it easier to use for overriding images with its optimized output.
    #
    # use: pngopt FILE1 [FILE2] [FILE3] ...
    #
    # If a file is optimized it outputs the filename with percentage reduction.
    #
    #
    # Example use:
    # To replace filea.png and fileb.png with its optimized counterpart:
    # $ pngopt filea.png fileb.png
    #

    while test $# -gt 0; do
    if [ ! -f $1 ] ; then
    echo "Error: Bad input file: $1" >&2
    shift
    continue
    fi

    tmpfile=$(tempfile --suffix ".png")
    pngcrush "$1" $tmpfile > /dev/null
    if ls -l $1 $tmpfile | awk '{print $5}' | xargs echo | awk '{exit !($1 > $2)}' ; then
    echo -n "$1 "
    ls -l $1 $tmpfile | awk '{print $5}' | xargs echo | \
    awk '{print "("(100 * ($1/$2 - 1))" %)"}'
    cp $tmpfile "$1"
    rm $tmpfile
    fi

    shift
    done