Mozilla JPEG Encoder
https://github.com/mozilla/mozjpeg
ウェブサイトの画像を一括で最適化してみようと思う
環境
1 2 3 4 5 6 |
Linux ServerName 5.13.0-30-generic #33~20.04.1-Ubuntu SMP Mon Feb 7 14:25:10 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux LSB Version: core-11.1.0ubuntu2-noarch:printing-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch Distributor ID: Ubuntu Description: Ubuntu 20.04.4 LTS Release: 20.04 Codename: focal |
make
1 2 3 4 5 6 7 8 |
$ wget https://github.com/mozilla/mozjpeg/archive/refs/tags/v4.0.3.tar.gz $ tar xvf v4.0.3.tar.gz $ cd mozjpeg-4.0.3/ $ cmake -G"Unix Makefiles" CFLAGS=-m64 LDFLAGS=-m64 . $ make $ ./jpegtran -version $ cp ./jpegtran ~/bin/mozjpeg $ cp ./cjpeg ~/bin/cjpeg |
一括変換スクリプト
嫌いなシェルスクリプトも書く
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
#!/bin/bash # 2022-02-10 checkComment () { comment=`identify -format %c "$1" 2> /dev/null` if [ $? -ne 0 ]; then return 99 fi # echo "Commnet: ${comment}" if [ "${comment}" = "MZJ" ]; then echo "Already compressed: $1" 1>&2 return 1 fi return 0 } switchArg () { # echo $1 if [ ! -e "$1" ]; then echo "Not exists: $1" 1>&2 return fi if [ -L "$1" ]; then echo "Symlink: ${1}" 1>&2 return fi if [ -d "$1" ]; then # echo dir IFS=$'\n' for line in `find "$1"`; do # echo ${line} if [ -d ${line} ]; then # echo "Directory: ${line}" continue fi if [ -L ${line} ]; then echo "Symlink: ${line}" 1>&2 continue fi checkComment ${line} if [ $? -ne 0 ]; then # already compressed or not jpeg format continue fi # echo "Compress ${line}" compress $line done else if [ ! -f "$1" ]; then echo "Not a file: $1" 1>&2 return fi compress $1 fi } compress () { fname=`basename $1` # 一応 拡張子をチェック echo ${fname} | grep -i -E '\.jpe?g$' > /dev/null 2>&1 if [ $? -ne 0 ]; then echo "File name mismatch: ${fname}" 1>&2 return 11 fi # 圧縮後も圧縮前のディレクトリー構造を維持する dir=${dst_dir}/`dirname $1` dir=`readlink -m "${dir}"` # ディレクトリーの正規化 mkdir -p "${dir}" if [ $? -ne 0 ]; then echo "Cannot make directory: ${dir}" 1>&2 return 1 fi outfile=`readlink -m "${dir}/${fname}"` # echo outfile: ${outfile} if [ -e $outfile ]; then echo "Output file exists and was skipped: ${outfile}" 1>&2 return 2 fi # CMDLINE1=(mozjpeg -copy none "$1") CMDLINE1=(mozjpeg -copy none) CMDLINE2=(cjpeg -quality ${quality} -optimize -outfile "${outfile}") if [ "${width}" != "" ]; then # echo width ${width} # 指定したサイズより幅が広い場合に縮小 CMDLINE3=(convert "$1" -resize ${width}x\> -quality 100 -) else CMDLINE3=(cat "$1") fi "${CMDLINE3[@]}" | "${CMDLINE1[@]}" | "${CMDLINE2[@]}" if [ $? -ne 0 ]; then echo "Compression failed: ${outfile}" 1>&2 else # 圧縮済みのマーク "MZJ" を書き込む mogrify -set comment "MZJ" "${outfile}" if [ $? -ne 0 ]; then echo "Comment 'MZJ' setting failed: ${outfile}" 1>&2 fi fi # サイズゼロのファイルは削除 if [ ! -s "${outfile}" ]; then # echo "FILE SIZE = 0" trash "${outfile}" echo "Removed file size was 0: ${outfile}" 1>&2 fi # echo "Compressed: ${1} => ${outfile}" echo -n "." return 0 } usage () { echo "Usage:" echo " jpeg85.sh -d <dest dir> [-w <width>] [-q <quality>] <dir|file...>" echo " default quality 85" exit 1 } main () { # コマンド引数解析 -d は後ろに値を持つ width="" dst_dir="" quality=85 while getopts d:w:q: OPT ; do case $OPT in d) dst_dir=$OPTARG ;; w) width=$OPTARG ;; q) quality=$OPTARG ;; *) usage ;; esac done shift $((OPTIND - 1)) # echo $OPT # echo $@ if [ "$dst_dir" = "" ]; then usage fi # 圧縮後のファイルを入れるディレクトリーを作成 if [ ! -d $dst_dir ]; then mkdir -p "${dst_dir}" if [ $? -ne 0 ]; then echo "Cannot make directory: ${dst_dir}" 1>&2 exit 1 fi fi echo "Destination directory: ${dst_dir}" # コマンド引数解析後の残りをループ for arg in $@; do switchArg $arg done } main $@ |