Skip to content

Instantly share code, notes, and snippets.

@danielantelo
Last active May 12, 2021 11:17
Show Gist options
  • Select an option

  • Save danielantelo/52f928d70d4d7da6f53b317780e4afc3 to your computer and use it in GitHub Desktop.

Select an option

Save danielantelo/52f928d70d4d7da6f53b317780e4afc3 to your computer and use it in GitHub Desktop.

Revisions

  1. danielantelo revised this gist Mar 21, 2017. 1 changed file with 18 additions and 3 deletions.
    21 changes: 18 additions & 3 deletions optimize.sh
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,14 @@ commandExists () {
    type "$1" &> /dev/null ;
    }

    installImageMagick() {
    if commandExists brew; then
    brew install imagemagick
    else
    apt-get install imagemagick
    fi
    }

    installJpegOptim() {
    if commandExists brew; then
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
    @@ -22,15 +30,21 @@ installOptiPng() {
    fi
    }

    resize() {
    RESIZE_PARAM='1200x640>'
    # resize different file types in parallel
    find . -name "*.jpg" -exec convert -resize $RESIZE_PARAM -verbose {} {} \; & find . -name "*.jpeg" -exec convert -resize $RESIZE_PARAM -verbose {} {} \; & find . -name "*.png" -exec convert -resize $RESIZE_PARAM -verbose {} {} \;
    }

    optimizePng() {
    # change -o5 to the desired levels of optimisation, the higher the number the slower it will go
    find . -name "*.png" -exec optipng -o5 -strip all {} \;
    }

    optimizeJpg() {
    # change -m65 to the desired quality level
    find . -name "*.jpg" -exec jpegoptim --max=65 -o -p --strip-all {} \;
    find . -name "*.jpeg" -exec jpegoptim --max=65 -o -p --strip-all {} \;
    QUALITY=65
    # optimize differen file types in parallel
    find . -name "*.jpg" -exec jpegoptim --max=$QUALITY -o -p --strip-all {} \; & find . -name "*.jpeg" -exec jpegoptim --max=$QUALITY -o -p --strip-all {} \;
    }

    if ! commandExists jpegoptim; then
    @@ -41,4 +55,5 @@ if ! commandExists optipng; then
    installOptiPng
    fi

    resize
    optimizeJpg & optimizePng
  2. danielantelo revised this gist Mar 20, 2017. 1 changed file with 5 additions and 16 deletions.
    21 changes: 5 additions & 16 deletions optimize.sh
    Original file line number Diff line number Diff line change
    @@ -22,25 +22,15 @@ installOptiPng() {
    fi
    }

    resizeImages() {
    if commandExists sips; then
    # change your desired max height here for the different image types
    find . -type f -name '*.png' -exec sips -Z 640 {} \;
    find . -type f -name '*.jpg' -exec sips -Z 640 {} \;
    find . -type f -name '*.jpeg' -exec sips -Z 640 {} \;
    fi
    # @TODO use something else when sips is not available
    }

    optimizePng() {
    # change -o4 to the desired levels of optimisation, the higher the number the slower it will go
    find . -name "*.png" -exec optipng -o4 -strip all {} \;
    # change -o5 to the desired levels of optimisation, the higher the number the slower it will go
    find . -name "*.png" -exec optipng -o5 -strip all {} \;
    }

    optimizeJpg() {
    # change -m65 to the desired quality level
    find . -name "*.jpg" -exec jpegoptim -m65 -o -p --strip-all {} \;
    find . -name "*.jpeg" -exec jpegoptim -m65 -o -p --strip-all {} \;
    find . -name "*.jpg" -exec jpegoptim --max=65 -o -p --strip-all {} \;
    find . -name "*.jpeg" -exec jpegoptim --max=65 -o -p --strip-all {} \;
    }

    if ! commandExists jpegoptim; then
    @@ -51,5 +41,4 @@ if ! commandExists optipng; then
    installOptiPng
    fi

    resizeImages
    optimizePng & optimizeJpg
    optimizeJpg & optimizePng
  3. danielantelo revised this gist Mar 17, 2017. 1 changed file with 14 additions and 6 deletions.
    20 changes: 14 additions & 6 deletions optimize.sh
    Original file line number Diff line number Diff line change
    @@ -6,26 +6,32 @@ commandExists () {

    installJpegOptim() {
    if commandExists brew; then
    # macOS install
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
    brew install jpegoptim
    else
    # ubuntu install
    apt-get install jpegoptim
    fi
    }

    installOptiPng() {
    if commandExists brew; then
    # macOS install
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
    brew install optipng
    else
    # ubuntu install
    apt-get install optipng
    fi
    }

    resizeImages() {
    if commandExists sips; then
    # change your desired max height here for the different image types
    find . -type f -name '*.png' -exec sips -Z 640 {} \;
    find . -type f -name '*.jpg' -exec sips -Z 640 {} \;
    find . -type f -name '*.jpeg' -exec sips -Z 640 {} \;
    fi
    # @TODO use something else when sips is not available
    }

    optimizePng() {
    # change -o4 to the desired levels of optimisation, the higher the number the slower it will go
    find . -name "*.png" -exec optipng -o4 -strip all {} \;
    @@ -34,14 +40,16 @@ optimizePng() {
    optimizeJpg() {
    # change -m65 to the desired quality level
    find . -name "*.jpg" -exec jpegoptim -m65 -o -p --strip-all {} \;
    find . -name "*.jpeg" -exec jpegoptim -m65 -o -p --strip-all {} \;
    }

    if ! commandExists jpegoptim; then
    installJpegOptim
    installJpegOptim
    fi

    if ! commandExists optipng; then
    installOptiPng
    installOptiPng
    fi

    resizeImages
    optimizePng & optimizeJpg
  4. danielantelo revised this gist Mar 17, 2017. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions optimize.sh
    Original file line number Diff line number Diff line change
    @@ -27,12 +27,12 @@ installOptiPng() {
    }

    optimizePng() {
    # change -o3 to the level of optimisation, the higher the number the slower it will go
    find . -name "*.png" -exec optipng -o3 -strip all {} \;
    # change -o4 to the desired levels of optimisation, the higher the number the slower it will go
    find . -name "*.png" -exec optipng -o4 -strip all {} \;
    }

    optimizeJpg() {
    # change -m65 for quality level of th output file
    # change -m65 to the desired quality level
    find . -name "*.jpg" -exec jpegoptim -m65 -o -p --strip-all {} \;
    }

    @@ -45,4 +45,3 @@ if ! commandExists optipng; then
    fi

    optimizePng & optimizeJpg

  5. danielantelo revised this gist Mar 17, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions optimize.sh
    Original file line number Diff line number Diff line change
    @@ -26,12 +26,12 @@ installOptiPng() {
    fi
    }

    optimizePNG() {
    optimizePng() {
    # change -o3 to the level of optimisation, the higher the number the slower it will go
    find . -name "*.png" -exec optipng -o3 -strip all {} \;
    }

    optimizeJPG() {
    optimizeJpg() {
    # change -m65 for quality level of th output file
    find . -name "*.jpg" -exec jpegoptim -m65 -o -p --strip-all {} \;
    }
    @@ -44,5 +44,5 @@ if ! commandExists optipng; then
    installOptiPng
    fi

    optimizePNG & optimizeJPG
    optimizePng & optimizeJpg

  6. danielantelo created this gist Mar 17, 2017.
    48 changes: 48 additions & 0 deletions optimize.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/bin/bash

    commandExists () {
    type "$1" &> /dev/null ;
    }

    installJpegOptim() {
    if commandExists brew; then
    # macOS install
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
    brew install jpegoptim
    else
    # ubuntu install
    apt-get install jpegoptim
    fi
    }

    installOptiPng() {
    if commandExists brew; then
    # macOS install
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
    brew install optipng
    else
    # ubuntu install
    apt-get install optipng
    fi
    }

    optimizePNG() {
    # change -o3 to the level of optimisation, the higher the number the slower it will go
    find . -name "*.png" -exec optipng -o3 -strip all {} \;
    }

    optimizeJPG() {
    # change -m65 for quality level of th output file
    find . -name "*.jpg" -exec jpegoptim -m65 -o -p --strip-all {} \;
    }

    if ! commandExists jpegoptim; then
    installJpegOptim
    fi

    if ! commandExists optipng; then
    installOptiPng
    fi

    optimizePNG & optimizeJPG