Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ Dependencies: [openssl](https://www.openssl.org/)
show-certificate google.com
```

### :camera: Optimize images size
Script: [optimage](optimage)
Dependencies:
```
apt-get update && apt-get -y install gcc libc6-dev libjpeg-progs trimage imagemagick pngquant
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, these installation instructions seem to be Ubuntu based.
Can you instead provide links to dependencies so that users can install them on their system on their own (if it's sufficiently feasible)? Thanks.

git clone https://github.com/kormoc/imgopt.git
cd imgopt
chmod +x imgopt
cp imgopt /usr/local/bin/
gcc -o jfifremove jfifremove.c
mv jfifremove /usr/local/bin/
wget http://static.jonof.id.au/dl/kenutils/pngout-20150319-linux.tar.gz
tar -zxf pngout-20150319-linux.tar.gz
cd pngout-20150319-linux/x86_64
mv pngout /usr/local/bin/
```

```sh
optimage <dir> --all #Recursively optimize all images in the specified directory
optimage <dir> #Recursively optimize all images modified in last 3 days
```

## 🤘🏻 SIMPLE BASH COMMANDS

These commands are so easy to use that creating a script for them would be overkill.
Expand Down
81 changes: 81 additions & 0 deletions optimage
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash
## Filename: optimage
## Function: Optimize images for web
## Author: Mateus WB
## Date: 2019-09-10
## Tools (dependencies): imgopt pngquant pngout imagemagick jfifremove
## Usage: optimage dir-to-optimize #(recursive)

#################### Installing dependencies - Debian9 (as root): ###############################
# apt-get update && apt-get -y install gcc libc6-dev libjpeg-progs trimage imagemagick pngquant #
# git clone https://github.com/kormoc/imgopt.git #
# cd imgopt #
# chmod +x imgopt #
# cp imgopt /usr/local/bin/ #
# gcc -o jfifremove jfifremove.c #
# mv jfifremove /usr/local/bin/ #
# wget http://static.jonof.id.au/dl/kenutils/pngout-20150319-linux.tar.gz #
# tar -zxf pngout-20150319-linux.tar.gz #
# cd pngout-20150319-linux/x86_64 #
# mv pngout /usr/local/bin/ #
#################################################################################################

#Usage evaluate
if [ "$1" == "" ] ;
then echo -e "Argument missing\nUsage optimage <fullpath-dir-to-optimize>"
exit
fi

#Run all images with argument or 3 days without it
if [ -n "$2" ] ; then
if [ "$2" == '--all' ] ;
then run_all=""
else echo "Invalid argument $2"
fi
else
run_all="-mtime +3"
fi

#Initialize environment
log_file="optimage.log"
dir_images="$1"
jpeg_quality="85"
start_size=`du -s $dir_images | awk -F ' ' '{print $1}'`
start_files=$(find $dir_images -type f ${run_all} | wc -l)
echo "[START - `date`] - Starting run ${run_all} | Dir $dir_images" | tee -a $log_file
echo "[`date`] - Initial dir size: $start_size" bytes | tee -a $log_file
echo "[`date`] - Files count: $start_files" | tee -a $log_file

#Start optimize
echo -n "[`date`] - Setting jpeg to quality = ${jpeg_quality} |" | tee -a $log_file
find $dir_images -type f ${run_all} -exec jpegoptim -m${jpeg_quality} {} \;
tmp_size=`du -s $dir_images | awk -F ' ' '{print $1}'`
echo " Optimized: $(( $start_size - $tmp_size )) bytes" | tee -a $log_file

echo -n "[`date`] - Running pngquant |" | tee -a $log_file
find $dir_images -type f ${run_all} -exec bash -c 'img_ext=".${1##*.}" ; if [ "$img_ext" != ".png" ] ; then img_ext="" ; fi ; pngquant -f --ext "$img_ext" "$1"' bash {} \;
tmp_size2=`du -s $dir_images | awk -F ' ' '{print $1}'`
echo " Optimized: $(( $tmp_size - $tmp_size2 )) bytes" | tee -a $log_file

echo -n "[`date`] - Running imgopt |" | tee -a $log_file
find $dir_images -type f ${run_all} -exec imgopt {} \;
tmp_size3=`du -s $dir_images | awk -F ' ' '{print $1}'`
echo " Optimized: $(( $tmp_size2 - $tmp_size3 )) bytes" | tee -a $log_file

echo -n "[`date`] - Running progressive |" | tee -a $log_file
find $dir_images -type f ${run_all} -exec jpegtran -copy none -progressive -outfile {} {} \;
tmp_size4=`du -s $dir_images | awk -F ' ' '{print $1}'`
echo " Optimized: $(( $tmp_size3 - $tmp_size4 )) bytes" | tee -a $log_file

finish_size=`du -s $dir_images | awk -F ' ' '{print $1}'`
finish_files=$(find $dir_images -type f ${run_all} | wc -l)

#Finished
echo -n "[`date`] - Final dir size: $finish_size bytes | Optimized: $(( $start_size - $finish_size )) bytes ( " | tee -a $log_file
echo "$(( $(( $start_size - $finish_size )) / $(( $start_size / 100 )) )) % )" | tee -a $log_file
echo "[`date`] - End files count: $finish_files" | tee -a $log_file
echo "[FINISH - `date`] - Run finished " | tee -a $log_file
if [ "$(( $start_size - $finish_size ))" -gt "0" ] ;
then exit 0;
else exit 1
fi