Compare commits

..

No commits in common. "master" and "v1.0" have entirely different histories.
master ... v1.0

19 changed files with 285 additions and 700 deletions

View File

@ -1,9 +0,0 @@
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

39
.gitattributes vendored
View File

@ -1,39 +0,0 @@
# Auto detect text files and perform LF normalization
* text=auto
# Documents
*.md text eol=lf
*.tex text diff=tex
*.adoc text
*.textile text
*.mustache text eol=lf
*.csv text
*.tab text
*.tsv text
*.txt text
*.sql text
# Scripts
*.bash text eol=lf
*.fish text eol=lf
*.sh text eol=lf
# Source files
# ============
*.pxd text diff=python
*.py text diff=python
*.py3 text diff=python
*.pyc text diff=python
*.pyd text diff=python
*.pyo text diff=python
*.pyw text diff=python
*.pyx text diff=python
*.pyz text diff=python
#
# Exclude files from exporting
#
.gitattributes export-ignore
.gitignore export-ignore

View File

@ -1,49 +0,0 @@
name: CI
on:
push:
branches:
- develop
- master
pull_request:
branches:
- master
release:
types: [published]
schedule:
- cron: '0 0 * * 0'
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Runs a set of commands using the runners shell
- name: Install prerequesites
run: |
sudo apt-get update
sudo apt-get install jpegoptim -y
sudo -E time bash scripts/install-webp.sh > /dev/null 2>&1
sudo -E time bash scripts/install-optipng.sh > /dev/null 2>&1
sudo wget -qO /usr/local/bin/avif https://github.com/Kagami/go-avif/releases/download/v0.1.0/avif-linux-x64
sudo chmod +x /usr/local/bin/avif
sudo cp optimize.sh /usr/local/bin/img-optimize
sudo chmod 755 /usr/local/bin/img-optimize
- name: Optimize Images
run: |
ls -alh images/
/usr/local/bin/img-optimize --avif
ls -alh images/
/usr/local/bin/img-optimize --webp
ls -alh images/
rm -f images/*.webp images/*.avif
/usr/local/bin/img-optimize --all
ls -alh images/

View File

@ -1,36 +0,0 @@
os: linux
dist: bionic
language: shell
cache:
apt: true
git:
quiet: true
before_script:
- sudo apt-get install tree -y
- sudo apt-get -qq autoremove --purge
addons:
apt:
update: true
script:
- sudo apt-get install jpegoptim -y
- sudo -E time bash scripts/install-webp.sh > /dev/null 2>&1
- sudo -E time bash scripts/install-optipng.sh > /dev/null 2>&1
- sudo wget -qO /usr/local/bin/avif https://github.com/Kagami/go-avif/releases/download/v0.1.0/avif-linux-x64
- sudo chmod +x /usr/local/bin/avif
- sudo cp optimize.sh /usr/local/bin/img-optimize
- sudo chmod 755 /usr/local/bin/img-optimize
- ls -alh images/
- /usr/local/bin/img-optimize --all -q
- ls -alh images/
- /usr/local/bin/img-optimize --jpg -q
- /usr/local/bin/img-optimize --png -q
- rm images/*.webp
- /usr/local/bin/img-optimize --webp
- tree -L 2 .
- rm images/*.webp
- /usr/local/bin/img-optimize --webp --quiet

View File

@ -1,38 +0,0 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased]
## [V2.0] - 2020-11-10
### Added
- quiet mode
- option --no-run-if-empty added to xargs
- scripts to compile optipng & libwebp from source
- Avif (AV1 Image Format) support
- Avoid optimizing the same image at the same moment [PR #12](https://github.com/VirtuBox/img-optimize/pull/12)
### Changed
- JPG are optimized as progressive JPG
- WebP and Avif newly created images are deleted in case of failure during conversion
## [V1.1] - 2019-04-05
### Added
- Interactive mode with `img-optimize -i`
- check if optipng, jpegoptim or cwebp executable before optimization
- additional install method
### Fixed
- images path replaced by current directory if empty
## [v1.0] - 2019-03-20
- Initial release

223
README.md
View File

@ -1,135 +1,88 @@
# Image optimization bash script ## Bash script to optimize your images and convert them in WebP
![img-optimize](https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/img-optimize.png) ![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png)
The changes from the [original repo](https://github.com/VirtuBox/img-optimize) is that the EXIF data is **preserved**. ### Prerequisite
All the credits to [VirtuBox](https://github.com/VirtuBox) * jpegoptim
* optipng
## Prerequisite * WebP
- jpegoptim for jpg optimization Debian/Ubuntu :
- optipng for png optimization
- cwebp for WebP conversion ```bash
- go-avif for Avif conversion sudo apt install jpegoptim optipng webp -y
```
### From APT repositories
Centos 7 :
Debian/Ubuntu :
```bash
```bash sudo yum install optipng jpegoptim libwebp-tools -y
sudo apt install jpegoptim optipng webp -y ```
```
---
Centos 7 :
### Installation
```bash
sudo yum install optipng jpegoptim libwebp-tools -y 1) Download the script
```
```bash
### Compile the latest release (optipng & libwebp) git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize
```
For Debian/Ubuntu (available in scripts folder) :
2) Add an alias in your bashrc
```bash
# optipng ```bash
curl -sL https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/scripts/install-optipng.sh | sudo -E bash echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc
source $HOME/.bashrc
# libwebp ```
curl -sL https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/scripts/install-webp.sh | sudo -E bash
``` ### Usage
### Go-Avif installation ```bash
Usage: img-optimize [options] <image path>
```bash Options:
sudo wget -qO /usr/local/bin/avif https://github.com/Kagami/go-avif/releases/download/v0.1.0/avif-linux-x64 --jpg <image path> ..... optimize all jpg images
sudo chmod +x /usr/local/bin/avif --png <image path> ..... optimize all png images
``` --webp <image path> ..... convert all images in webp
--nowebp <image path> ..... optimize all png & jpg images
-------------------------------------------------------------------------------- --all <image path> ..... optimize all images (png + jpg + webp)
Other options :
## Installation -h, --help, help ... displays this help information
Examples:
1) Clone the repository optimize all jpg images in /var/www/images
img-optimize --jpg /var/www/images
```bash
git clone https://gitea.avc.cx/borekon/img-optimize.git $HOME/.img-optimize ```
```
### Update the script
2) Install the script
To update the script, just run :
**Method 1** : Add an alias in .bashrc
```bash
With this method img-optimize can only be used by the current user git -C $HOME/.img-optimize pull
```bash source .bashrc
echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc ```
source $HOME/.bashrc
``` ### Setup daily cronjob
**Method 2** : Add an alias to the script in /usr/local/bin You copy the scripts to /etc/cron.daily :
With this method img-optimize can be used by all users ```bash
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron
```bash cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron
sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize
sudo chmod +x /usr/local/bin/img-optimize chmod +x /etc/cron.daily/jpg-png-cron
``` chmod +x /etc/cron.daily/webp-cron
```
## Usage
Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts.
```bash
Bash script to optimize your images and convert them in WebP ### Warning
Usage: img-optimize [options] <images path>
If images path isn't defined, img-optimize will use the current directory Conversion process can take a while, you can use `tmux` to launch the script and be able to close your ssh connection without interrupting conversion. Then just use `tmux attach` to login back in your tmux session.
Options:
--jpg ..... optimize all jpg images ### Credits
--png ..... optimize all png images
--webp ..... convert all images in webp WebP conversion script was inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
--avif ..... convert all images in avif
--std ..... optimize all png & jpg images
--next ..... convert all images in webp & avif
--all ..... optimize all images (png + jpg + webp + avif)
-i, --interactive ..... run img-optimize in interactive mode
-q, --quiet ..... run image optimization quietly
--path <images path> ..... define images path
Other options :
-h, --help, help ... displays this help information
--cmin [+|-]<n> ... File's status was last changed n minutes ago.
act find cmin argument (+n : greater than n, -n : less than n, n : exactly n)
Examples:
optimize all jpg images in /var/www/images
img-optimize --jpg --path /var/www/images
```
### Update the script
To update the script, just run :
```bash
git -C $HOME/.img-optimize pull
```
### Setup daily cronjob
You just have to copy the scripts to /etc/cron.daily :
```bash
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron
chmod +x /etc/cron.daily/jpg-png-cron
chmod +x /etc/cron.daily/webp-cron
```
Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts.
## Warning
Conversion process can take a while, you can use `tmux` to launch the script and be able to close your ssh connection without interrupting conversion. Then just use `tmux attach` to login back in your tmux session.
## Credits
- WebP conversion script was inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french)

View File

@ -10,7 +10,7 @@ sites="/var/www/yoursite.tld/images \
for site in $sites; do for site in $sites; do
# optimize jpg images created in the last 24 hours # optimize jpg images created in the last 24 hours
find "$site" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --strip-none -m82 >> /var/log/jpg-png-cron.log 2>&1 find "$site" -ctime 0 -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --strip-all -m82 >> /var/log/jpg-png-cron.log 2>&1
# optimize png images created in the last 24 hours # optimize png images created in the last 24 hours
find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 >> /var/log/jpg-png-cron.log 2>&1 find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all >> /var/log/jpg-png-cron.log 2>&1
done done

View File

@ -1,133 +1,88 @@
# Image optimization bash script ## Bash script to optimize your images and convert them in WebP
![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png) ![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png)
[![Travis](https://travis-ci.org/VirtuBox/img-optimize.svg?branch=master)](https://travis-ci.org/VirtuBox/img-optimize) ![GitHub](https://img.shields.io/github/license/VirtuBox/img-optimize.svg) ![GitHub release](https://img.shields.io/github/release/VirtuBox/img-optimize.svg) ![GitHub last commit](https://img.shields.io/github/last-commit/VirtuBox/img-optimize.svg) ![Github stars](https://img.shields.io/github/stars/VirtuBox/img-optimize.svg) ### Prerequisite
## Prerequisite * jpegoptim
* optipng
- jpegoptim for jpg optimization * WebP
- optipng for png optimization
- cwebp for WebP conversion Debian/Ubuntu :
- go-avif for Avif conversion
```bash
### From APT repositories sudo apt install jpegoptim optipng webp -y
```
Debian/Ubuntu :
Centos 7 :
```bash
sudo apt install jpegoptim optipng webp -y ```bash
``` sudo yum install optipng jpegoptim libwebp-tools -y
```
Centos 7 :
---
```bash
sudo yum install optipng jpegoptim libwebp-tools -y ### Installation
```
1) Download the script
### Compile the latest release (optipng & libwebp)
```bash
For Debian/Ubuntu (available in scripts folder) : git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize
```
```bash
# optipng 2) Add an alias in your bashrc
curl -sL git.io/fjddn | sudo -E bash
```bash
# libwebp echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc
curl -sL git.io/fjdd6 | sudo -E bash source $HOME/.bashrc
``` ```
### Go-Avif installation ### Usage
```bash ```bash
sudo wget -qO /usr/local/bin/avif https://github.com/Kagami/go-avif/releases/download/v0.1.0/avif-linux-x64 Usage: img-optimize [options] <image path>
sudo chmod +x /usr/local/bin/avif Options:
``` --jpg <image path> ..... optimize all jpg images
--png <image path> ..... optimize all png images
-------------------------------------------------------------------------------- --webp <image path> ..... convert all images in webp
--nowebp <image path> ..... optimize all png & jpg images
## Installation --all <image path> ..... optimize all images (png + jpg + webp)
Other options :
1) Clone the repository -h, --help, help ... displays this help information
Examples:
```bash optimize all jpg images in /var/www/images
git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize img-optimize --jpg /var/www/images
```
```
2) Install the script
### Update the script
**Method 1** : Add an alias in .bashrc
To update the script, just run :
With this method img-optimize can only be used by the current user
```bash
```bash git -C $HOME/.img-optimize pull
echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc
source $HOME/.bashrc source .bashrc
``` ```
**Method 2** : Add an alias to the script in /usr/local/bin ### Setup daily cronjob
With this method img-optimize can be used by all users You copy the scripts to /etc/cron.daily :
```bash ```bash
sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron
sudo chmod +x /usr/local/bin/img-optimize cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron
```
chmod +x /etc/cron.daily/jpg-png-cron
## Usage chmod +x /etc/cron.daily/webp-cron
```
```bash
Bash script to optimize your images and convert them in WebP Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts.
Usage: img-optimize [options] <images path>
If images path isn't defined, img-optimize will use the current directory ### Warning
Options:
--jpg ..... optimize all jpg images Conversion process can take a while, you can use `tmux` to launch the script and be able to close your ssh connection without interrupting conversion. Then just use `tmux attach` to login back in your tmux session.
--png ..... optimize all png images
--webp ..... convert all images in webp ### Credits
--avif ..... convert all images in avif
--std ..... optimize all png & jpg images WebP conversion script was inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
--next ..... convert all images in webp & avif
--all ..... optimize all images (png + jpg + webp + avif)
-i, --interactive ..... run img-optimize in interactive mode
-q, --quiet ..... run image optimization quietly
--path <images path> ..... define images path
Other options :
-h, --help, help ... displays this help information
--cmin [+|-]<n> ... File's status was last changed n minutes ago.
act find cmin argument (+n : greater than n, -n : less than n, n : exactly n)
Examples:
optimize all jpg images in /var/www/images
img-optimize --jpg --path /var/www/images
```
### Update the script
To update the script, just run :
```bash
git -C $HOME/.img-optimize pull
```
### Setup daily cronjob
You just have to copy the scripts to /etc/cron.daily :
```bash
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron
chmod +x /etc/cron.daily/jpg-png-cron
chmod +x /etc/cron.daily/webp-cron
```
Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts.
## Warning
Conversion process can take a while, you can use `tmux` to launch the script and be able to close your ssh connection without interrupting conversion. Then just use `tmux attach` to login back in your tmux session.
## Credits
- WebP conversion script was inspired by this [DigitalOcean Community Tutorial](https://www.digitalocean.com/community/tutorials/how-to-create-and-serve-webp-images-to-speed-up-your-website)
- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 498 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 399 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 645 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 316 KiB

21
install-optipng.sh Normal file
View File

@ -0,0 +1,21 @@
#!/bin/bash
# install prerequisites
sudo apt-get install build-essential libpng-dev -y
# go into /usr/local/src and remove previous optipng folder/archive
cd /usr/local/src || exit 1
rm -rf optipng*
# get the latest optipng release link
OPTIPNGLATEST=$(wget http://optipng.sourceforge.net/ -O - | grep tar.gz | awk -F '["]' '{print $4}')
# download and extract optipng
wget $OPTIPNGLATEST -O optipng.tar.gz
tar -xf optipng.tar.gz
cd optipng-* || exit 1
# configure and compile optipng
./configure --prefix=/usr
make -j "$(nproc)"
sudo make install

18
install-webp.sh Normal file
View File

@ -0,0 +1,18 @@
#!/bin/bash
# install prerequisites
sudo apt-get install build-essential libjpeg-dev libpng-dev libtiff-dev libgif-dev libwebp-dev -y
# go into /usr/local/src and remove previous libwebp folder/archive
cd /usr/local/src || exit
rm -rf libwebp*
# download and extract latest libwebp sources
wget https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.0.0.tar.gz -O libwebp.tar.gz
tar xvzf libwebp.tar.gz
cd libwebp-* || exit
# configure libwebp and launch compilation
./configure
make -j "$(nproc)"
sudo make install

View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/bin/bash
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# img-optimize- Image optimization bash script # img-optimize- Image optimization bash script
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
@ -7,40 +7,29 @@
# Author: VirtuBox # Author: VirtuBox
# License: M.I.T # License: M.I.T
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Version 2.0 - 2020-11-10 # Version 1.0 - 2019-03-20
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
#!/bin/bash
CSI='\033[' CSI='\033['
CEND="${CSI}0m" CEND="${CSI}0m"
CGREEN="${CSI}1;32m" CGREEN="${CSI}1;32m"
FIND_ARGS=""
PNG_ARGS=""
JPG_ARGS=""
WEBP_ARGS="-metadata all"
IMG_PATH="$PWD"
_help() { _help() {
echo "Bash script to optimize your images and convert them in WebP " echo "Bash script to optimize your images and convert them in WebP "
echo "Usage: img-optimize [options] <images path>" echo "Usage: img-optimize [options] <image path>"
echo "If images path isn't defined, img-optimize will use the current directory "
echo " Options:" echo " Options:"
echo " --jpg ..... optimize all jpg images" echo " --jpg <image path> ..... optimize all jpg images"
echo " --png ..... optimize all png images" echo " --png <image path> ..... optimize all png images"
echo " --webp ..... convert all images in webp" echo " --webp <image path> ..... convert all images in webp"
echo " --avif ..... convert all images in avif" echo " --nowebp <image path> ..... optimize all png & jpg images"
echo " --std ..... optimize all png & jpg images" echo " --all <image path> ..... optimize all images (png + jpg + webp)"
echo " --next ..... convert all images in webp & avif"
echo " --all ..... optimize all images (png + jpg + webp + avif)"
echo " -i, --interactive ..... run img-optimize in interactive mode"
echo " -q, --quiet ..... run image optimization quietly"
echo " --path <images path> ..... define images path"
echo " Other options :" echo " Other options :"
echo " -h, --help, help ... displays this help information" echo " -h, --help, help ... displays this help information"
echo " --cmin [+|-]<n> ... File's status was last changed n minutes ago."
echo " act find cmin argument (+n : greater than n, -n : less than n, n : exactly n)"
echo "Examples:" echo "Examples:"
echo " optimize all jpg images in /var/www/images" echo " optimize all jpg images in /var/www/images"
echo " img-optimize --jpg --path /var/www/images" echo " img-optimize --jpg /var/www/images"
echo "" echo ""
return 0 return 0
} }
@ -49,83 +38,54 @@ _help() {
# Parse script arguments # Parse script arguments
################################## ##################################
if [ "${#}" = "0" ]; then if [ "${#}" = "0" ]; then
_help _help
exit 1 exit 1
else else
while [ "$#" -gt 0 ]; do while [ ${#} -gt 0 ]; do
case "$1" in case "${1}" in
--jpg) --jpg)
JPG_OPTIMIZATION="y" JPG_OPTIMIZATION="y"
;; if [ "$2" ]; then
--png) IMG_PATH=$2
PNG_OPTIMIZATION="y" fi
;;
--std)
JPG_OPTIMIZATION="y"
PNG_OPTIMIZATION="y"
WEBP_OPTIMIZATION="n"
AVIF_OPTIMIZATION="n"
;;
--next)
AVIF_OPTIMIZATION="y"
WEBP_OPTIMIZATION="y"
;;
--webp)
WEBP_OPTIMIZATION="y"
;;
--avif)
AVIF_OPTIMIZATION="y"
;;
--all)
PNG_OPTIMIZATION="y"
JPG_OPTIMIZATION="y"
WEBP_OPTIMIZATION="y"
AVIF_OPTIMIZATION="y"
;;
-i | --interactive)
INTERACTIVE_MODE="1"
;;
-q | --quiet)
PNG_ARGS=" -quiet"
JPG_ARGS=" --quiet"
WEBP_ARGS=" -quiet"
;;
--cmin)
if [ -n "$2" ]; then
FIND_ARGS="$2"
shift shift
fi
;; ;;
--path) --png)
if [ -n "$2" ]; then PNG_OPTIMIZATION="y"
IMG_PATH="$2" IMG_PATH=$2
shift shift
fi
;; ;;
-h | --help | help) --nowebp)
_help JPG_OPTIMIZATION="y"
exit 1 PNG_OPTIMIZATION="y"
WEBP_OPTIMIZATION="n"
IMG_PATH=$2
shift
;; ;;
*) ;; --webp)
WEBP_OPTIMIZATION="y"
IMG_PATH=$2
shift
;;
--all)
PNG_OPTIMIZATION="y"
JPG_OPTIMIZATION="y"
WEBP_OPTIMIZATION="y"
IMG_PATH=$2
shift
;;
-h | --help | help)
_help
exit 1
;;
*) ;;
esac esac
shift shift
done done
fi fi
##################################
# Prevent multi execution on same directory
##################################
lock=$(echo -n "$IMG_PATH" | md5sum| cut -d" " -f1)
if [ -f "/tmp/$lock" ]; then
echo "$IMG_PATH yet in progress"
exit 1
else
touch "/tmp/$lock"
fi
################################## ##################################
# Welcome # Welcome
################################## ##################################
@ -134,44 +94,26 @@ echo ""
echo "Welcome to optimize.sh image optimization script." echo "Welcome to optimize.sh image optimization script."
echo "" echo ""
if [ "$INTERACTIVE_MODE" = "1" ]; then if [ -z "$JPG_OPTIMIZATION" ] && [ -z "$PNG_OPTIMIZATION" ] && [ -z "$WEBP_OPTIMIZATION" ]; then
if [ -z "$IMG_PATH" ]; then
echo "What is the path of images you want to optimize ?"
echo "Images path (eg. /var/www/images):"
read -r IMG_PATH
fi
if [ -z "$JPG_OPTIMIZATION" ]; then if [ -z "$JPG_OPTIMIZATION" ]; then
echo "" echo ""
echo "Do you want to optimize all jpg images in $IMG_PATH ? (y/n)" echo "Do you want to optimize all jpg images in $1 ? (y/n)"
while [[ $JPG_OPTIMIZATION != "y" && $JPG_OPTIMIZATION != "n" ]]; do while [[ $JPG_OPTIMIZATION != "y" && $JPG_OPTIMIZATION != "n" ]]; do
echo "Select an option [y/n]: " read -p "Select an option [y/n]: " JPG_OPTIMIZATION
read -r JPG_OPTIMIZATION
done done
fi fi
if [ -z "$PNG_OPTIMIZATION" ]; then if [ -z "$PNG_OPTIMIZATION" ]; then
echo "" echo ""
echo "Do you want to optimize all png images in $IMG_PATH (it may take a while) ? (y/n)" echo "Do you want to optimize all png images in $1 (it may take a while) ? (y/n)"
while [[ $PNG_OPTIMIZATION != "y" && $PNG_OPTIMIZATION != "n" ]]; do while [[ $PNG_OPTIMIZATION != "y" && $PNG_OPTIMIZATION != "n" ]]; do
echo "Select an option [y/n]: " read -p "Select an option [y/n]: " PNG_OPTIMIZATION
read -r PNG_OPTIMIZATION
done done
fi fi
if [ -z "$WEBP_OPTIMIZATION" ]; then if [ -z "$WEBP_OPTIMIZATION" ]; then
echo "" echo ""
echo "Do you want to convert all jpg & png images to WebP in $IMG_PATH ? (y/n)" echo "Do you want to convert all jpg & png images to WebP in $1 ? (y/n)"
while [[ $WEBP_OPTIMIZATION != "y" && $WEBP_OPTIMIZATION != "n" ]]; do while [[ $WEBP_OPTIMIZATION != "y" && $WEBP_OPTIMIZATION != "n" ]]; do
echo "Select an option [y/n]: " read -p "Select an option [y/n]: " WEBP_OPTIMIZATION
read -r WEBP_OPTIMIZATION
done
echo ""
echo ""
fi
if [ -z "$AVIF_OPTIMIZATION" ]; then
echo ""
echo "Do you want to convert all jpg & png images to AVIF in $IMG_PATH ? (y/n)"
while [[ $AVIF_OPTIMIZATION != "y" && $AVIF_OPTIMIZATION != "n" ]]; do
echo "Select an option [y/n]: "
read -r AVIF_OPTIMIZATION
done done
echo "" echo ""
echo "" echo ""
@ -184,115 +126,44 @@ fi
# optimize jpg # optimize jpg
if [ "$JPG_OPTIMIZATION" = "y" ]; then if [ "$JPG_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v jpegoptim)" ] && {
echo "Error: jpegoptim isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1
}
echo -ne ' jpg optimization [..]\r' echo -ne ' jpg optimization [..]\r'
cd "$IMG_PATH" || exit 1 cd $IMG_PATH || exit 1
if [ -n "$FIND_ARGS" ]; then find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --strip-all -m82
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" -p -P --strip-none --all-progressive -m82
else
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -r -0 jpegoptim "$JPG_ARGS" -p -P --strip-none -m82 --all-progressive
fi
echo -ne " jpg optimization [${CGREEN}OK${CEND}]\\r" echo -ne " jpg optimization [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
fi fi
if [ "$PNG_OPTIMIZATION" = "y" ]; then if [ "$PNG_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v optipng)" ] && {
echo "Error: optipng isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1
}
# optimize png # optimize png
echo -ne ' png optimization [..]\r' echo -ne ' png optimization [..]\r'
cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1) cd $IMG_PATH || exit 1
if [ -n "$FIND_ARGS" ]; then find . -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all
find . -type f -iname '*.png' -cmin "$FIND_ARGS" -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5
else
find . -type f -iname '*.png' -print0 | xargs -r -0 optipng "$PNG_ARGS" -o5
fi
echo -ne " png optimization [${CGREEN}OK${CEND}]\\r" echo -ne " png optimization [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
fi fi
if [ "$WEBP_OPTIMIZATION" = "y" ]; then if [ "$WEBP_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v cwebp)" ] && {
echo "Error: cwebp isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1
}
# convert png to webp # convert png to webp
echo -ne ' png to webp conversion [..]\r' echo -ne ' png to webp conversion [..]\r'
cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1) cd $IMG_PATH || exit 1
if [ -n "$FIND_ARGS" ]; then find . -type f -iname "*.png" -print0 | xargs -0 -I {} \
find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt {} -o {}.webp; }'
bash -c "[ ! -f '{}.webp' ] && { cwebp -z 9 -mt $WEBP_ARGS '{}' -o '{}.webp'; }"
else
find . -type f -iname "*.png" -print0 | xargs -0 -r -I {} \
bash -c "[ ! -f '{}.webp' ] && { cwebp -z 9 -mt $WEBP_ARGS '{}' -o '{}.webp'; }"
fi
echo -ne " png to webp conversion [${CGREEN}OK${CEND}]\\r" echo -ne " png to webp conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
# convert jpg to webp # convert jpg to webp
echo -ne ' jpg to webp conversion [..]\r' echo -ne ' jpg to webp conversion [..]\r'
cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1) cd $IMG_PATH || exit 1
if [ -n "$FIND_ARGS" ]; then find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -I {} \
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \ bash -c '[ ! -f "{}.webp" ] && { cwebp -q 82 -mt {} -o {}.webp; }'
bash -c "[ ! -f '{}.webp' ] && { cwebp $WEBP_ARGS -q 82 -mt '{}' -o '{}.webp || rm -f '{}.webp''; }"
else
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -r -I {} \
bash -c "[ ! -f '{}.webp' ] && { cwebp $WEBP_ARGS -q 82 -mt '{}' -o '{}.webp' || rm -f '{}.webp'; }"
fi
echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r" echo -ne " jpg to webp conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n' echo -ne '\n'
fi fi
if [ "$AVIF_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v avif)" ] && {
echo "Error: avif isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1
}
# convert png to avif
echo -ne ' png to avif conversion [..]\r'
cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1)
if [ -n "$FIND_ARGS" ]; then
find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \
bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; }"
else
find . -type f -iname "*.png" -print0 | xargs -0 -r -I {} \
bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; }"
fi
echo -ne " png to avif conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'
# convert jpg to avif
echo -ne ' jpg to avif conversion [..]\r'
cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1)
if [ -n "$FIND_ARGS" ]; then
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \
bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; } "
else
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -r -I {} \
bash -c "[ ! -f '{}.avif' ] && { avif -e '{}' -o '{}.avif' || rm -f '{}.avif'; }"
fi
echo -ne " jpg to avif conversion [${CGREEN}OK${CEND}]\\r"
echo -ne '\n'
fi
# We're done ! # We're done !
echo "" echo ""
echo -e " ${CGREEN}Image optimization performed successfully !${CEND}" echo -e " ${CGREEN}Image optimization performed successfully !${CEND}"
echo "" echo ""
# Free ressource
rm "/tmp/$lock"

BIN
optipng

Binary file not shown.

View File

@ -1,31 +0,0 @@
#!/usr/bin/env bash
#----------------------------------------------------------------------------
# img-optimize- optipng compilation script
#----------------------------------------------------------------------------
# Website: https://virtubox.net
# GitHub: https://github.com/VirtuBox/img-optimize
# Author: VirtuBox
# License: M.I.T
# ----------------------------------------------------------------------------
# install prerequisites
export DEBIAN_FRONTEND=noninteractive
apt update && apt-get install --assume-yes build-essential libpng-dev zlib1g-dev curl pigz jpegoptim
# get the latest optipng release link
OPTIPNGLATEST=$(curl -sL optipng.sourceforge.net | grep tar.gz | awk -F '["]' '{print $4}')
# go into /usr/local/src and remove previous optipng folder/archive
cd /usr/local/src || exit 1
rm -rf optipng*
# download and extract optipng
curl -sL "$OPTIPNGLATEST" | tar -I pigz -xf -
cd optipng-* || exit 1
# configure and compile optipng
./configure
make -j "$(nproc)"
strip --strip-unneeded /usr/local/src/optipng-*/src/optipng/optipng
make install
ldconfig

View File

@ -1,31 +0,0 @@
#!/usr/bin/env bash
#----------------------------------------------------------------------------
# img-optimize- libwebp compilation script
#----------------------------------------------------------------------------
# Website: https://virtubox.net
# GitHub: https://github.com/VirtuBox/img-optimize
# Author: VirtuBox
# License: M.I.T
# ----------------------------------------------------------------------------
# install prerequisites
export DEBIAN_FRONTEND=noninteractive
apt-get update && apt-get install --assume-yes build-essential libjpeg-dev libpng-dev libtiff-dev libgif-dev libwebp-dev tar pigz curl
# get the latest release number
LATEST_WEBP=$(curl https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html 2>&1 | grep ".tar.gz\"" | awk -F '["]' '{print $2}' | sort -r | head -n 1 2>&1)
# go into /usr/local/src and remove previous libwebp folder/archive
cd /usr/local/src || exit
rm -rf libwebp*
# download and extract latest libwebp sources
curl -sL "https:$LATEST_WEBP" | tar -I pigz -xf -
cd libwebp-* || exit
# configure libwebp and launch compilation
./configure
make -j "$(nproc)"
strip --strip-unneeded /usr/local/src/libwebp-*/examples/.libs/{cwebp,dwebp}
make install
ldconfig