Skip to content

Instantly share code, notes, and snippets.

@VirtuBox
Last active October 30, 2021 08:26
Show Gist options
  • Save VirtuBox/e00d67ed177aa0d5af0edf2d087ef43c to your computer and use it in GitHub Desktop.
Save VirtuBox/e00d67ed177aa0d5af0edf2d087ef43c to your computer and use it in GitHub Desktop.
Optimize your jpg & png images with jpegoptim and optipng on linux

JPG and PNG image bulk optimization

Install jpegoptim and optipng

sudo apt update && apt install jpegoptim optipng -y

compile the latest release of optipng

Install dependencies

sudo apt-get install build-essential libpng-dev -y

compile optipng

cd /usr/local/src
OPTIPNGLATEST=$(wget http://optipng.sourceforge.net/ -qO - | grep tar.gz | awk -F "[\"]" '{print $4}')
wget $OPTIPNGLATEST -qO optipng.tar.gz
tar -xf optipng.tar.gz
cd optipng-*

If gcc8.1 available :

export CC="/usr/bin/gcc-8"
export CXX="/usr/bin/gc++-8"

If gcc7 available :

export CC="/usr/bin/gcc-7"
export CXX="/usr/bin/gc++-7"

Compile optipng

./configure --prefix=/usr
make -j "$(nproc)"
sudo make install

JPG optimization

cd /path/to/your/image/folder
find . -iname "*.jp*" -print0 | xargs -0 jpegoptim --strip-all -m76

PNG optimization

cd /path/to/your/image/folder
find . -iname '*.png' -print0 | xargs -0 optipng -o7 -preserve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment