Giter VIP home page Giter VIP logo

img-optimize's Introduction

Image optimization bash script

img-optimize

Travis GitHub GitHub release GitHub last commit Github stars

Prerequisite

  • jpegoptim for jpg optimization
  • optipng for png optimization
  • cwebp for WebP conversion
  • go-avif for Avif conversion

From APT repositories

Debian/Ubuntu :

sudo apt install jpegoptim optipng webp -y

Centos 7 :

sudo yum install optipng jpegoptim libwebp-tools -y

Compile the latest release (optipng & libwebp)

For Debian/Ubuntu (available in scripts folder) :

# optipng
curl -sL git.io/fjddn | sudo -E bash

# libwebp
curl -sL git.io/fjdd6 | sudo -E bash

Go-Avif installation

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

Installation

  1. Clone the repository
git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize
  1. Install the script

Method 1 : Add an alias in .bashrc

With this method img-optimize can only be used by the current user

echo "alias img-optimize=$HOME/.img-optimize/optimize.sh" >> $HOME/.bashrc
source $HOME/.bashrc

Method 2 : Add an alias to the script in /usr/local/bin

With this method img-optimize can be used by all users

sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize
sudo chmod +x /usr/local/bin/img-optimize

Usage

Bash script to optimize your images and convert them in WebP
Usage: img-optimize [options] <images path>
If images path isn't defined, img-optimize will use the current directory
  Options:
       --jpg ..... optimize all jpg images
       --png ..... optimize all png images
       --webp ..... convert all images in webp
       --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 :

git -C $HOME/.img-optimize pull

Setup daily cronjob

You just have to copy the scripts to /etc/cron.daily :

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.