45 lines
957 B
Bash
45 lines
957 B
Bash
|
#!/usr/bin/env zsh
|
||
|
|
||
|
# optimize png and jpg files
|
||
|
|
||
|
calc_reduction()
|
||
|
{
|
||
|
SIZE_PRE="$1"
|
||
|
SIZE_POST="$2"
|
||
|
PERECENT=` echo "scale=2; 100 - (100 * $SIZE_POST / $SIZE_PRE)" | bc `
|
||
|
RESULT="$(( $SIZE_PRE / 1000 ))-$(( ( $SIZE_PRE - $SIZE_POST ) / 1000 )) kb \
|
||
|
($PERECENT%)
|
||
|
"
|
||
|
echo " $RESULT"
|
||
|
}
|
||
|
|
||
|
for FILENAME in "$@"
|
||
|
do
|
||
|
FILETYPE=` file -b --mime-type "$FILENAME" `
|
||
|
echo "$FILENAME : $FILETYPE"
|
||
|
|
||
|
SIZE_PRE=` stat "$FILENAME" -c '%s' `
|
||
|
|
||
|
case "$FILETYPE" in
|
||
|
'image/png')
|
||
|
mogrify "$FILENAME"
|
||
|
pngcrush -nolimits -s "$FILENAME" "$FILENAME.tmp.png"
|
||
|
if [ -f "$FILENAME.tmp.png" ] \
|
||
|
&& [ -s "$FILENAME.tmp.png" ]
|
||
|
then
|
||
|
mv "$FILENAME.tmp.png" "$FILENAME"
|
||
|
else
|
||
|
rm "$FILENAME.tmp.png"
|
||
|
fi
|
||
|
;;
|
||
|
|
||
|
'image/jpeg')
|
||
|
jpegoptim "$FILENAME" -qsftp
|
||
|
;;
|
||
|
esac
|
||
|
|
||
|
SIZE_POST=` stat "$FILENAME" -c '%s' `
|
||
|
[ $SIZE_PRE -gt $SIZE_POST ] \
|
||
|
&& echo ` calc_reduction "$SIZE_PRE" "$SIZE_POST" `
|
||
|
done
|