Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 -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