Compare commits

..

56 Commits
v1.1 ... master

Author SHA1 Message Date
101b24a5c7 Typo in the question to convert all files to AVIF format
pull from 
https://github.com/VirtuBox/img-optimize/pull/19
2025-01-28 11:00:47 +01:00
ecebc0dadf typo fix 2025-01-28 10:58:38 +01:00
c5fc39d5d1 Preserve metadata when converting to webp
Added to $WEBP_ARGS the '-metadata string all' that copies the metadata from source to webp destination
2025-01-28 10:53:54 +01:00
f8f6188edc optipng already complied for x86_64 2024-07-10 14:30:53 +02:00
27dfe70ce7 Do not change owner when optimizing jpg 2024-04-14 21:47:15 +00:00
8f9c99465a strip-none is not supported by optipng 2023-07-11 13:47:01 +00:00
c3dd0dd535 Keep EXIF data in jpg cron 2023-07-11 13:45:02 +00:00
5fcf709922 Actualizar 'README.md' 2023-07-04 09:11:51 +00:00
8364f01c21 Actualizar 'README.md' 2023-03-13 13:17:41 +00:00
79da33911a Keep all the EXIF/IPTC/etc markers 2023-01-30 09:19:46 +00:00
e39529cd99 Actualizar 'README.md' 2023-01-30 09:16:58 +00:00
VirtuBox
c78ff855ef
Merge pull request #17 from stafyniaksacha/master
Remove lock file on fail
2022-12-02 12:05:32 +01:00
Sacha STAFYNIAK
ae15dcf6b7
Remove lock file on fail 2022-10-20 15:54:37 +02:00
VirtuBox
6c8104ca2c
Merge pull request #15 from VirtuBox/develop
v2.0
2020-11-11 13:38:51 +01:00
VirtuBox
2696d188d4
Merge remote-tracking branch 'origin/develop' into develop 2020-11-11 12:57:00 +01:00
VirtuBox
e91fee0836
Update Changelog and Readme 2020-11-11 12:56:44 +01:00
VirtuBox
e7d9dee890
Update main.yml 2020-11-11 12:47:08 +01:00
VirtuBox
ec68a935bf
Create main.yml 2020-11-11 12:41:24 +01:00
VirtuBox
55dbd153a9
Merge pull request #12 from Webelys/enable-lock
Don't run twice on same directory on same moment
2020-11-11 12:30:01 +01:00
VirtuBox
f445537361
Fix typo 2020-11-10 20:30:31 +01:00
VirtuBox
5e1c6d1da2
Update img-optimize
* avif support
2020-11-10 18:49:15 +01:00
VirtuBox
cbe45eea8e
Fix travis 2020-10-19 14:07:33 +02:00
VirtuBox
a77944a525
Remove avif file if conversion fail 2020-10-19 13:59:16 +02:00
VirtuBox
716c541168
Update travis 2020-10-18 19:11:16 +02:00
VirtuBox
32e33d1f34
Add avif support 2020-10-18 18:49:56 +02:00
Camille Lafitte
092b337494 Don't forget to retrieve only md5 part
* md5sum return spacing and - in more , remove it
2020-03-05 15:36:31 +01:00
Camille Lafitte
0ac427fdae Don't run twice on same directory on same moment
* We can't run optimize script twice on same instant
2020-03-05 15:19:51 +01:00
VirtuBox
1b458c423c
update readme 2019-12-08 14:52:02 +01:00
VirtuBox
54457264a4
update travis 2019-12-08 02:35:34 +01:00
VirtuBox
c1b6708ec2
Update script 2019-12-06 01:02:27 +01:00
VirtuBox
673889e740
Fix bash -c 2019-08-29 18:27:51 +02:00
VirtuBox
0fa994d9ec
update github page 2019-08-17 11:41:13 +02:00
VirtuBox
5738bccae7
Fix dead link 2019-08-17 11:35:08 +02:00
VirtuBox
b843c45144
Fix duplicate link 2019-08-17 11:31:01 +02:00
VirtuBox
a4a3c99be7
Add scripts to compile optipng & libwebp 2019-08-15 23:44:27 +02:00
VirtuBox
9d02d67ae7
Fix $FIND_ARGS 2019-07-26 12:53:21 +02:00
VirtuBox
967857a421
Try to fix quiet 2019-07-19 11:41:00 +02:00
VirtuBox
0607ede078
update travis for testing 2019-07-19 10:56:12 +02:00
VirtuBox
bb5f42b037
Fix travis image in README 2019-07-19 10:39:22 +02:00
VirtuBox
c80ebccbd2
Fix 2nd argument not valid due to image path 2019-07-19 10:37:30 +02:00
VirtuBox
5d9d604436
Merge pull request #8 from Webelys/cmin-patch
Cmin patch
2019-07-19 10:23:52 +02:00
Camille Lafitte
f437df5cf3 Be more explicit about cmin numeric value
* We can/must use +/- prefix
2019-07-17 18:00:19 +02:00
Camille Lafitte
f0542d557c Add cmin support
* Follow cmin find argument
* cf #5
2019-07-17 17:59:53 +02:00
Camille Lafitte
35d692f17f Simplify quiet mode
* Provide an unique optional argument
* set quiet mode if required

related to #6 and 2cd9570f8a
2019-07-17 17:37:24 +02:00
VirtuBox
3dca2073ac
Extract archive non verbose 2019-07-12 16:31:38 +02:00
VirtuBox
9413616c72
Strip binaries before make install 2019-07-12 15:23:17 +02:00
VirtuBox
13bf8d7752
Update webp install script & compile libwebp 2019-07-12 14:29:29 +02:00
VirtuBox
666a93baad
set png level to o5 2019-07-12 14:13:14 +02:00
VirtuBox
20154dd39c
Fix cwebp command 2019-07-12 14:11:01 +02:00
VirtuBox
94b9c21492
Add xargs flag --no-run-if-empty 2019-07-12 14:07:39 +02:00
VirtuBox
9b152bb6c8
Fix travis 2019-07-12 13:59:55 +02:00
VirtuBox
f6a5576adb
Add travis 2019-07-12 13:58:53 +02:00
VirtuBox
743c1b95d9
update readme and changelog 2019-07-12 13:46:09 +02:00
VirtuBox
2cd9570f8a
Add quiet mode 2019-07-12 13:44:21 +02:00
VirtuBox
347490b7e7
Merge pull request #3 from camlafit/patch-1
Prevent error with file and space
2019-05-17 12:25:50 +02:00
Km
82d73c67a8
Prevent error with file and space
Preserve name spacing when webp run
2019-05-17 10:57:49 +02:00
19 changed files with 623 additions and 305 deletions

9
.editorconfig Normal file
View File

@ -0,0 +1,9 @@
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 Normal file
View File

@ -0,0 +1,39 @@
# 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

49
.github/workflows/main.yml vendored Normal file
View File

@ -0,0 +1,49 @@
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/

36
.travis.yml Normal file
View File

@ -0,0 +1,36 @@
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

@ -6,8 +6,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [Unreleased] ## [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 ### 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 ## [V1.1] - 2019-04-05
### Added ### Added

244
README.md
View File

@ -1,109 +1,135 @@
# Image optimization bash script # Image optimization bash script
![img-optimize](https://raw.githubusercontent.com/VirtuBox/img-optimize/master/img-optimize.png) ![img-optimize](https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/img-optimize.png)
![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) 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 for jpg optimization ## Prerequisite
- optipng for png optimization
- cwebp for WebP conversion - jpegoptim for jpg optimization
- optipng for png optimization
Debian/Ubuntu : - cwebp for WebP conversion
- go-avif for Avif conversion
```bash
sudo apt install jpegoptim optipng webp -y ### From APT repositories
```
Debian/Ubuntu :
Centos 7 :
```bash
```bash sudo apt install jpegoptim optipng webp -y
sudo yum install optipng jpegoptim libwebp-tools -y ```
```
Centos 7 :
--------------------------------------------------------------------------------
```bash
## Installation sudo yum install optipng jpegoptim libwebp-tools -y
```
1) Clone the repository
### Compile the latest release (optipng & libwebp)
```bash
git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize For Debian/Ubuntu (available in scripts folder) :
```
```bash
2) Install the script # optipng
curl -sL https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/scripts/install-optipng.sh | sudo -E bash
**Method 1** : Add an alias in .bashrc
# libwebp
With this method img-optimize can only be used by the current user curl -sL https://gitea.avc.cx/borekon/img-optimize/raw/branch/master/scripts/install-webp.sh | sudo -E bash
```
```bash
echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc ### Go-Avif installation
source $HOME/.bashrc
``` ```bash
sudo wget -qO /usr/local/bin/avif https://github.com/Kagami/go-avif/releases/download/v0.1.0/avif-linux-x64
**Method 2** : Copy the script in /usr/local/bin sudo chmod +x /usr/local/bin/avif
```
With this method img-optimize can be used by all users
--------------------------------------------------------------------------------
```bash
sudo cp $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize ## Installation
```
1) Clone the repository
## Usage
```bash
```bash git clone https://gitea.avc.cx/borekon/img-optimize.git $HOME/.img-optimize
Usage: img-optimize [options] <images path> ```
If images path is empty, img-optimize will use the current directory
Options: 2) Install the script
--jpg <images path> ..... optimize all jpg images
--png <images path> ..... optimize all png images **Method 1** : Add an alias in .bashrc
--webp <images path> ..... convert all images in webp
--nowebp <images path> ..... optimize all png & jpg images With this method img-optimize can only be used by the current user
--all <images path> ..... optimize all images (png + jpg + webp)
-i, --interactive ... run img-optimize in interactive mode ```bash
Other options : echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc
-h, --help, help ... displays this help information source $HOME/.bashrc
Example: ```
optimize all jpg images in /var/www/images
img-optimize --jpg /var/www/images **Method 2** : Add an alias to the script in /usr/local/bin
```
With this method img-optimize can be used by all users
## Update the script
```bash
To update the script, just run : sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize
sudo chmod +x /usr/local/bin/img-optimize
```bash ```
git -C $HOME/.img-optimize pull
``` ## Usage
If you previously used the 2nd install method, you have to copy the script again to update it ```bash
Bash script to optimize your images and convert them in WebP
```bash Usage: img-optimize [options] <images path>
sudo cp $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize -f If images path isn't defined, img-optimize will use the current directory
``` Options:
--jpg ..... optimize all jpg images
## Setup daily cronjob --png ..... optimize all png images
--webp ..... convert all images in webp
You copy the scripts to /etc/cron.daily : --avif ..... convert all images in avif
--std ..... optimize all png & jpg images
```bash --next ..... convert all images in webp & avif
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron --all ..... optimize all images (png + jpg + webp + avif)
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron -i, --interactive ..... run img-optimize in interactive mode
-q, --quiet ..... run image optimization quietly
chmod +x /etc/cron.daily/jpg-png-cron --path <images path> ..... define images path
chmod +x /etc/cron.daily/webp-cron Other options :
``` -h, --help, help ... displays this help information
--cmin [+|-]<n> ... File's status was last changed n minutes ago.
Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts. act find cmin argument (+n : greater than n, -n : less than n, n : exactly n)
Examples:
## Warning optimize all jpg images in /var/www/images
img-optimize --jpg --path /var/www/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. ```
## Credits ### Update the script
- 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) To update the script, just run :
- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french) ```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-all -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-none -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 -strip all >> /var/log/jpg-png-cron.log 2>&1 find "$site" -ctime 0 -type f -iname '*.png' -print0 | xargs -0 optipng -o7 >> /var/log/jpg-png-cron.log 2>&1
done done

View File

@ -1,109 +1,133 @@
# Image optimization bash script # Image optimization bash script
![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)
![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) [![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 for jpg optimization - jpegoptim for jpg optimization
- optipng for png optimization - optipng for png optimization
- cwebp for WebP conversion - cwebp for WebP conversion
- go-avif for Avif conversion
Debian/Ubuntu :
### From APT repositories
```bash
sudo apt install jpegoptim optipng webp -y Debian/Ubuntu :
```
```bash
Centos 7 : 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
### Compile the latest release (optipng & libwebp)
1) Clone the repository
For Debian/Ubuntu (available in scripts folder) :
```bash
git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize ```bash
``` # optipng
curl -sL git.io/fjddn | sudo -E bash
2) Install the script
# libwebp
**Method 1** : Add an alias in .bashrc curl -sL git.io/fjdd6 | sudo -E bash
```
With this method img-optimize can only be used by the current user
### Go-Avif installation
```bash
echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc ```bash
source $HOME/.bashrc 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
```
**Method 2** : Copy the script in /usr/local/bin
--------------------------------------------------------------------------------
With this method img-optimize can be used by all users
## Installation
```bash
sudo cp $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize 1) Clone the repository
```
```bash
## Usage git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize
```
```bash
Usage: img-optimize [options] <images path> 2) Install the script
If images path is empty, img-optimize will use the current directory
Options: **Method 1** : Add an alias in .bashrc
--jpg <images path> ..... optimize all jpg images
--png <images path> ..... optimize all png images With this method img-optimize can only be used by the current user
--webp <images path> ..... convert all images in webp
--nowebp <images path> ..... optimize all png & jpg images ```bash
--all <images path> ..... optimize all images (png + jpg + webp) echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc
-i, --interactive ... run img-optimize in interactive mode source $HOME/.bashrc
Other options : ```
-h, --help, help ... displays this help information
Example: **Method 2** : Add an alias to the script in /usr/local/bin
optimize all jpg images in /var/www/images
img-optimize --jpg /var/www/images With this method img-optimize can be used by all users
```
```bash
## Update the script sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize
sudo chmod +x /usr/local/bin/img-optimize
To update the script, just run : ```
```bash ## Usage
git -C $HOME/.img-optimize pull
``` ```bash
Bash script to optimize your images and convert them in WebP
If you previously used the 2nd install method, you have to copy the script again to update it Usage: img-optimize [options] <images path>
If images path isn't defined, img-optimize will use the current directory
```bash Options:
sudo cp $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize -f --jpg ..... optimize all jpg images
``` --png ..... optimize all png images
--webp ..... convert all images in webp
## Setup daily cronjob --avif ..... convert all images in avif
--std ..... optimize all png & jpg images
You copy the scripts to /etc/cron.daily : --next ..... convert all images in webp & avif
--all ..... optimize all images (png + jpg + webp + avif)
```bash -i, --interactive ..... run img-optimize in interactive mode
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/jpg-png-cron -q, --quiet ..... run image optimization quietly
cp $HOME/.img-optimize/crons/jpg-png-cron.sh /etc/cron.daily/webp-cron --path <images path> ..... define images path
Other options :
chmod +x /etc/cron.daily/jpg-png-cron -h, --help, help ... displays this help information
chmod +x /etc/cron.daily/webp-cron --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:
Then just edit your websites path set with the variables `sites` at the beginning of the cron scripts. optimize all jpg images in /var/www/images
img-optimize --jpg --path /var/www/images
## 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. ### Update the script
## Credits To update the script, just run :
- 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) ```bash
git -C $HOME/.img-optimize pull
- Tutorial about webp conversion available on [jesuisadmin.fr](https://jesuisadmin.fr/convertir-vos-images-en-webp-nginx/) (in french) ```
### 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)

BIN
images/1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 498 KiB

BIN
images/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 399 KiB

BIN
images/2.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 645 KiB

BIN
images/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

BIN
images/3.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 316 KiB

View File

@ -1,21 +0,0 @@
#!/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

View File

@ -1,18 +0,0 @@
#!/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

@ -7,29 +7,40 @@
# Author: VirtuBox # Author: VirtuBox
# License: M.I.T # License: M.I.T
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Version 1.1 - 2019-04-05 # Version 2.0 - 2020-11-10
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
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] <images path>"
echo "If images path isn't defined, img-optimize will use the current directory " echo "If images path isn't defined, img-optimize will use the current directory "
echo " Options:" echo " Options:"
echo " --jpg <images path> ..... optimize all jpg images" echo " --jpg ..... optimize all jpg images"
echo " --png <images path> ..... optimize all png images" echo " --png ..... optimize all png images"
echo " --webp <images path> ..... convert all images in webp" echo " --webp ..... convert all images in webp"
echo " --nowebp <images path> ..... optimize all png & jpg images" echo " --avif ..... convert all images in avif"
echo " --all <images path> ..... optimize all images (png + jpg + webp)" echo " --std ..... optimize all png & jpg images"
echo " -i, --interactive ... run img-optimize in interactive mode" 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 /var/www/images" echo " img-optimize --jpg --path /var/www/images"
echo "" echo ""
return 0 return 0
} }
@ -47,46 +58,52 @@ else
case "$1" in case "$1" in
--jpg) --jpg)
JPG_OPTIMIZATION="y" JPG_OPTIMIZATION="y"
if [ "$2" ]; then
IMG_PATH=$2
shift
fi
;; ;;
--png) --png)
PNG_OPTIMIZATION="y" PNG_OPTIMIZATION="y"
if [ "$2" ]; then
IMG_PATH=$2
shift
fi
;; ;;
--nowebp) --std)
JPG_OPTIMIZATION="y" JPG_OPTIMIZATION="y"
PNG_OPTIMIZATION="y" PNG_OPTIMIZATION="y"
WEBP_OPTIMIZATION="n" WEBP_OPTIMIZATION="n"
if [ "$2" ]; then AVIF_OPTIMIZATION="n"
IMG_PATH=$2 ;;
shift --next)
fi AVIF_OPTIMIZATION="y"
WEBP_OPTIMIZATION="y"
;; ;;
--webp) --webp)
WEBP_OPTIMIZATION="y" WEBP_OPTIMIZATION="y"
if [ "$2" ]; then ;;
IMG_PATH=$2 --avif)
shift AVIF_OPTIMIZATION="y"
fi
;; ;;
--all) --all)
PNG_OPTIMIZATION="y" PNG_OPTIMIZATION="y"
JPG_OPTIMIZATION="y" JPG_OPTIMIZATION="y"
WEBP_OPTIMIZATION="y" WEBP_OPTIMIZATION="y"
if [ "$2" ]; then AVIF_OPTIMIZATION="y"
IMG_PATH=$2
shift
fi
;; ;;
-i | --interactive) -i | --interactive)
INTERACTIVE_MODE="1" INTERACTIVE_MODE="1"
;; ;;
-q | --quiet)
PNG_ARGS=" -quiet"
JPG_ARGS=" --quiet"
WEBP_ARGS=" -quiet"
;;
--cmin)
if [ -n "$2" ]; then
FIND_ARGS="$2"
shift
fi
;;
--path)
if [ -n "$2" ]; then
IMG_PATH="$2"
shift
fi
;;
-h | --help | help) -h | --help | help)
_help _help
exit 1 exit 1
@ -97,6 +114,18 @@ else
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
################################## ##################################
@ -108,34 +137,44 @@ echo ""
if [ "$INTERACTIVE_MODE" = "1" ]; then if [ "$INTERACTIVE_MODE" = "1" ]; then
if [ -z "$IMG_PATH" ]; then if [ -z "$IMG_PATH" ]; then
echo "What is the path of images you want to optimize ?" echo "What is the path of images you want to optimize ?"
read -p "Images path (eg. /var/www/images): " IMG_PATH echo "Images path (eg. /var/www/images):"
read -r IMG_PATH
fi 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 $IMG_PATH ? (y/n)"
while [[ $JPG_OPTIMIZATION != "y" && $JPG_OPTIMIZATION != "n" ]]; do while [[ $JPG_OPTIMIZATION != "y" && $JPG_OPTIMIZATION != "n" ]]; do
read -p "Select an option [y/n]: " JPG_OPTIMIZATION echo "Select an option [y/n]: "
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 $IMG_PATH (it may take a while) ? (y/n)"
while [[ $PNG_OPTIMIZATION != "y" && $PNG_OPTIMIZATION != "n" ]]; do while [[ $PNG_OPTIMIZATION != "y" && $PNG_OPTIMIZATION != "n" ]]; do
read -p "Select an option [y/n]: " PNG_OPTIMIZATION echo "Select an option [y/n]: "
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 $IMG_PATH ? (y/n)"
while [[ $WEBP_OPTIMIZATION != "y" && $WEBP_OPTIMIZATION != "n" ]]; do while [[ $WEBP_OPTIMIZATION != "y" && $WEBP_OPTIMIZATION != "n" ]]; do
read -p "Select an option [y/n]: " WEBP_OPTIMIZATION echo "Select an option [y/n]: "
read -r WEBP_OPTIMIZATION
done done
echo "" echo ""
echo "" echo ""
fi fi
else if [ -z "$AVIF_OPTIMIZATION" ]; then
if [ -z "$IMG_PATH" ]; then echo ""
IMG_PATH="$PWD" 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
echo ""
echo ""
fi fi
fi fi
@ -147,11 +186,17 @@ fi
if [ "$JPG_OPTIMIZATION" = "y" ]; then if [ "$JPG_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v jpegoptim)" ] && { [ -z "$(command -v jpegoptim)" ] && {
echo "Error: jpegoptim isn't installed" echo "Error: jpegoptim isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1 exit 1
} }
echo -ne ' jpg optimization [..]\r' echo -ne ' jpg optimization [..]\r'
cd "$IMG_PATH" || exit 1 cd "$IMG_PATH" || exit 1
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 jpegoptim --preserve --strip-all -m82 if [ -n "$FIND_ARGS" ]; then
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'
@ -159,41 +204,95 @@ fi
if [ "$PNG_OPTIMIZATION" = "y" ]; then if [ "$PNG_OPTIMIZATION" = "y" ]; then
[ -z "$(command -v optipng)" ] && { [ -z "$(command -v optipng)" ] && {
echo "Error: optipng isn't installed" echo "Error: optipng isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1 exit 1
} }
# optimize png # optimize png
echo -ne ' png optimization [..]\r' echo -ne ' png optimization [..]\r'
cd "$IMG_PATH" || exit 1 cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1)
find . -type f -iname '*.png' -print0 | xargs -0 optipng -o7 -strip all if [ -n "$FIND_ARGS" ]; then
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)" ] && { [ -z "$(command -v cwebp)" ] && {
echo "Error: cwebp isn't installed" echo "Error: cwebp isn't installed"
# Free ressource
rm "/tmp/$lock"
exit 1 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" || exit 1 cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1)
find . -type f -iname "*.png" -print0 | xargs -0 -I {} \ if [ -n "$FIND_ARGS" ]; then
bash -c '[ ! -f "{}.webp" ] && { cwebp -z 9 -mt {} -o {}.webp; }' find . -type f -iname "*.png" -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \
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" || exit 1 cd "$IMG_PATH" || (rm "/tmp/$lock" && exit 1)
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | xargs -0 -I {} \ if [ -n "$FIND_ARGS" ]; then
bash -c '[ ! -f "{}.webp" ] && { cwebp -q 82 -mt {} -o {}.webp; }' find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -cmin "$FIND_ARGS" -print0 | xargs -0 -r -I {} \
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 Normal file

Binary file not shown.

View File

@ -0,0 +1,31 @@
#!/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

31
scripts/install-webp.sh Normal file
View File

@ -0,0 +1,31 @@
#!/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