ZImageOptimizer
Simple image optimizer for JPEG, PNG and GIF images on Linux, MacOS and FreeBSD.
Install / Use
/learn @zevilz/ZImageOptimizerREADME
zImageOptimizer

Simple bash script for lossless image optimizing JPEG, PNG and GIF images in a specified directory include subdirectories on Linux, MacOS and FreeBSD.
Features
- lossless image optimization with a small image size in the output;
- works recursively;
- checks optimization tools after the start;
- option for automatic install dependencies and optimization tools if one or more of it is not found (see supported distributions here);
- readable output info and total info after optimization;
- no limit for a file size (limited only by hardware);
- no limit for a number of files;
- no limit for a length of a filename (limited only by a file system);
- supports for special characters (except slashes and back slashes), spaces, not latin characters in a filename;
- supports for search of images changed in a certain period of time;
- supports for use of the special time marker for search only new files (based on last modify time).
- supports extensions via special hooks
Requirements
- bash 4+
Tools
JPEG:
- jpegoptim
- jpegtran, djpeg and cjpeg (from libjpeg library)
PNG:
- pngcrush (v1.7.22+)
- optipng (v0.7+)
- pngout
- advpng (from AdvanceCOMP library)
GIF:
One or more tools required for optimization. djpeg/cjpeg does not support CMYK colorspace and does not participate in the optimization of such images.
Notice: Optimization via djpeg/cjpeg is currently temporarily disabled to prevent an increase in size of output files in lossless mode. Later, djpeg/cjpeg options for lossless compression and small output file size will be selected.
Usage
Usual usage
bash zImageOptimizer.sh -p /path/to/files
or
bash zImageOptimizer.sh --path=/path/to/files
Supported options:
-h (--help)- shows a help message;-v (--version)- shows a script version;-p (--path)- specify a full path to the input directory (usage:-p <dir> | --path=<dir>);-q (--quiet)- executes the script without any questions and users actions;-l (--less)- don't shows the optimizing process;-c (--check-only)- checks tools with an opportunity to install dependencies;-t (--time)- sets the period for which to look for files by last modified time (usage:-t <period> | --time=<period>);-n (--new-only)- use the time marker file for looking new images only;-m (--time-marker)- sets the custom full path or the custom filename of a time marker file (usage:-m <name|path> | --time-marker=<name|path>);-tmp (--tmp-path)- sets the custom directory full path for temporary files (usage:-tmp <dir> | --tmp-path=<dir>);-e (--exclude)- a comma separated parts list of paths to files for an exclusion from search (the script removes from the search files in the full path of which includes any value from the list; usage:-e <list> | --exclude=<list>);-ext (--extensions)- a comma separated list of script's extensions to enable. Script's extensions disabled by default. Useallto enable all found extensions (usage:-ext <list> | --extensions=<list> | -ext all | --extensions=all).--unlock- manually delete target dir from lockfile if previous script launch was interrupted incorrectly or killed by system.
Notices:
- you may combine options;
-h (--help)option ignore all other options;-v (--version)option ignore all other options (except for-h (--help));-c (--check-only)option ignore all other options (except for-h (--help)and-v (--version));- path in
-p (--path)and-tmp (--tmp-path)options may be with and without a slash in the end of the path; - it is impossible to use together
-t (--time)and-n (--new-only)options; - you must use
-m (--time-marker)option with-n (--new-only)option; - you must use
--unlockoption with-p (--path)option (just add--unlockoption in next run); - you can install dependencies without any questions in check mode (
-c (--check-only)) using-q (--quiet)option.
Recommendation: use GNU Screen or analogs if there are many images in an input directory, because the optimization may can take long time.
After starting optimization, the script creates special temporary lock file (/tmp/zio.lock by default), where path to working directory is added. After optimization is finished, the script deletes this file (or deletes current path to working directory from the file with several parallel optimizations). This is done to prevent cycling optimization and avoid conflicts during optimization for longer than period between optimizations. Notice: if the script is terminated abnormally, you should delete lock file manually.
Excluding folders/files from search
bash zImageOptimizer.sh -p /path/to/files -e <list>
Example:
bash zImageOptimizer.sh -p /path/to/files -e /var/www/test.com,backup,uploads/orig.png
Usage with set the period
bash zImageOptimizer.sh -p /path/to/files -t <period>
Supported periods:
- minutes (10m, 30m etc.),
- hours (1h, 10h etc.),
- days (1d, 30d etc.).
Example:
bash zImageOptimizer.sh -p /path/to/files -t 15d
Usage with the time marker (recommended for a cron usage)
bash zImageOptimizer.sh -p /path/to/files -n
Notice: by default the time marker file created in working a directory which set in -p (--path) option with a filename .timeMarker.
Custom time marker name
Use -m (--time-marker) option and set a new filename if you want to change the time marker filename:
bash zImageOptimizer.sh -p /path/to/files -n -m myCustomMarkerName
Path to time marker will be /path/to/files/myCustomMarkerName
Custom time marker path and name
Use -m (--time-marker) option and set a new path and filename if you want to change the time marker path:
bash zImageOptimizer.sh -p /path/to/files -n -m /path/to/marker/directory/markerName
Path to the time marker will be /path/to/marker/directory/markerName
Cron usage
Using default time marker:
0 0 * * * /bin/bash zImageOptimizer.sh -p /first/directory -q -n
0 1 * * * /bin/bash zImageOptimizer.sh -p /second/directory -q -n
Using custom time marker path and filename:
0 0 * * * /bin/bash zImageOptimizer.sh -p /first/directory -q -n -m /path/to/first/marker/firstMarkerName
0 1 * * * /bin/bash zImageOptimizer.sh -p /second/directory -q -n -m /path/to/second/marker/secondMarkerName
Also you may collect all markers in own directory:
0 0 * * * /bin/bash zImageOptimizer.sh -p /first/directory -q -n -m /path/to/markers/directory/firstMarkerName
0 1 * * * /bin/bash zImageOptimizer.sh -p /second/directory -q -n -m /path/to/markers/directory/secondMarkerName
Notice: use -l (--less) option if you want exclude optimizing process in cron email messages
Manually create/modify the time marker file
You may manually create the time marker file or change it last modified time:
touch -m /path/to/marker/markerName
If you want to create the marker with specify time or change marker last modified time with specify time:
touch -t [[CC]YY]MMDDhhmm[.SS]
where:
CC– 2 first digits of the year,YY– 2 last digits of the year,MM– month,DD– date,hh– hours in 24 format,mm– minutes,SS– seconds.
Example:
touch -t 201712041426.00 /path/to/marker/markerName
Usage with custom path to the temporary files directory
bash zImageOptimizer.sh -p /path/to/files -tmp /custom/path/to/temporary/directory
Docker
Docker image provided by Varun Batra.
If you have built the docker container then dependencies are installed already. To use docker either start with mounting to /work/images folder which is compressed every 15 days for example:
docker run -u "$UID:$GID" --volume /path/to/images:/work/images -v /etc/passwd:/etc/passwd:ro -v /etc/group:/etc/group:ro varunbatrait/zimageoptimizer
Or you can come up with your own CMD for example:
# mkdir /mnt/ImagesHundred/marker
docker run -it -u "$UID:$GID" -d --volume /mnt/ImagesHundred/marker:/work/marker --volume /mnt/ImagesHundred/images/:/work/images/ -v /etc/passwd:/etc/passwd:ro -v /etc/group:/etc/group:ro varunbatrait/zimageoptimizer ./zImageOptimizer.sh -p /work/images/ -q -n -m /work/marker/marker
Automatical installing dependencies
Notice: curent user must be root or user with sudo access.
Start the script in the optimization mode (-p|--path) or the checking tools mode (-c|--check-only, recommended) if you want to install dependencies automatically. It will check installed tools and printing a choise option dialog if one or more tools not found. Select Install dependencies and exit option by typing a appropriate number and press enter. The script will install dependencies based on your platform, distribution and package manager. You may need to enter a password and confirm actions during installation dependencies. Restart script to recheck installed tools after that.
Supported on:
- DEB-based linux distributions (i686/amd64)
- Debian 7+
- Ubuntu 14.04+
- RPM-based linux distributions (i686/x86_64)
- RHEL 6+
- CentOS 6+
- Fedora 24+
- FreeBSD 10+ (i686/amd64)
- MacOS 10+
Tested on:
- DEB-based linux distributions
- Debian 7.11 i686 minimal
- Debi
