Last active
March 7, 2016 07:12
-
-
Save m13253/11347868 to your computer and use it in GitHub Desktop.
Optimize large-size images for web by interlacing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # This program is released under MIT license. | |
| # This program comes with absolutely no warranty and may cause damages, use it at your risk. | |
| # The author of this program is StarBrilliant <echo bTEzMjUzQGhvdG1haWwuY29tCg== | base64 -d> | |
| if [ "$#" -eq 0 ] | |
| then | |
| echo "Optimize large-size images for web by interlacing" | |
| echo "Usage: $0 1.png 2.jpg ..." | |
| echo "optipng and libjpeg are required." | |
| exit | |
| fi | |
| jpegtran_output="" | |
| jpegtran_output="$(mktemp)" | |
| jpegtran_scans="" | |
| jpegtran_scans="$(mktemp)" | |
| if [ -z "$jpegtran_output" -o -z "$jpegtran_scans" ] | |
| then | |
| echo 'Fatal: mktemp is unavailable.' | |
| return 1 | |
| fi | |
| cat >"$jpegtran_scans"<<'EOF' | |
| # This file is adapted from jpgcrush | |
| # progressive jpeg scan specification (formatted for `jpegtran -scans`) | |
| # optimized for size, not for actual progressive transmission. | |
| # this averages about 1% smaller than jpegtran's default scan order, and about 1% bigger than image-specific brute-force search. | |
| 0: 0 0 0 0 ; | |
| 1 2: 0 0 0 0 ; | |
| 0: 1 8 0 2 ; | |
| 1: 1 8 0 0 ; | |
| 2: 1 8 0 0 ; | |
| 0: 9 63 0 2 ; | |
| 0: 1 63 2 1 ; | |
| 0: 1 63 1 0 ; | |
| 1: 9 63 0 0 ; | |
| 2: 9 63 0 0 ; | |
| EOF | |
| for i in "$@" | |
| do | |
| basename_i="$(basename "$i")" | |
| if [ "$(basename "$i" .png)" != "$basename_i" -o "$(basename "$i" .PNG)" != "$basename_i" ] | |
| then | |
| echo "optipng -i1 $i" | |
| optipng -i1 "$i" | |
| elif [ "$(basename "$i" .jpg)" != "$basename_i" -o "$(basename "$i" .JPG)" != "$basename_i" -o "$(basename "$i" .jpeg)" != "$basename_i" -o "$(basename "$i" .JPEG)" != "$basename_i" ] | |
| then | |
| true >"$jpegtran_output" | |
| echo "jpegtran -copy all -optimize -progressive -scans "$jpegtran_scans" -outfile $jpegtran_output $i" | |
| jpegtran -copy all -optimize -progressive -scans "$jpegtran_scans" -outfile "$jpegtran_output" "$i" | |
| if [ "$?" -eq 0 ] | |
| then | |
| echo "mv $jpegtran_output $i" | |
| mv "$jpegtran_output" "$i" | |
| fi | |
| else | |
| echo "Error: unsupported file, only *.png, *.jpg, *.jpeg are supported." | |
| fi | |
| done | |
| rm -f "$jpegtran_output" "$jpegtran_scans" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stylecheck...
[ ... -o ]is not POSIX and not readable. Since you are using bash, I suggest using[[ ... || ... ]]. Otherwise[ ... ] || [ ... ].basenameto remove suffixes is not interesting. Since you seem like trying to do some pattern matching, why not usecase?*.[pP][nN][gG]works perfectly (so does*.png|PNG.)[[can be used for glob matching:[[ $a == *.avi.?z ]]matchesaaa.avi.gzandaaa.avi.xz.!=works too.set -xandset +x?StaticChk...
return:20.