diff --git a/README.md b/README.md index 8b53328..3034c09 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,19 @@ -# zImageOptimizer [![Version](https://img.shields.io/badge/version-v0.9.3-orange.svg)](https://github.com/zevilz/zImageOptimizer/releases/tag/0.9.3) [![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://www.paypal.me/zevilz) +# zImageOptimizer [![Version](https://img.shields.io/badge/version-v0.9.5-orange.svg)](https://github.com/zevilz/zImageOptimizer/releases/tag/0.9.5) [![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://www.paypal.me/zevilz) -Simple bash script for lossless image optimizing JPEG, PNG and GIF images in specified directory include subdirectories on Linux, MacOS and FreeBSD. +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 small image size in output; -- script work recursively; -- checks optimization tools after start; -- option for automatic install dependences and optimization tools if one or more of it not found (see supported distributions [here](https://github.com/zevilz/zImageOptimizer#automatical-installing-dependences)); -- readable information in output and total info after optimization; -- no limit for file size (limit only by hardware); -- no limit for number of files; -- no limit for length of file name (limit on by file system); -- support for special characters (except slashes and back slashes), spaces, not latin characters in file name; -- support of search of the images changed in a certain period of time; -- support of use of a special time marker for search only new files (based on last modify time). +- lossless image optimization with a small image size in the output; +- works recursively; +- checks optimization tools after the start; +- option for automatic install dependences and optimization tools if one or more of it is not found (see supported distributions [here](https://github.com/zevilz/zImageOptimizer#automatical-installing-dependences)); +- 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). ## Tools JPEG: @@ -29,7 +29,7 @@ PNG: GIF: - [gifsicle](http://www.lcdf.org/gifsicle/) -One or more tools required for optimization. +One or more tools required for optimization. djpeg/cjpeg does not support CMYK colorspace and does not participate in the optimization of such images. ## Usage @@ -43,28 +43,28 @@ bash zImageOptimizer.sh --path=/path/to/files ``` Supported options: -- -h (--help) - shows help; -- -v (--version) - shows script version; -- -p (--path) - specify full path to input directory (usage: `-p | --path=`); -- -q (--quiet) - execute script without any questions and users actions; -- -l (--less) - don't show optimizing process; -- -c (--check-only) - check tools with an opportunity to install dependences; -- -t (--time) - set period for which to look for files by last modified time (usage: `-t | --time=`); -- -n (--new-only) - use time marker file for looking new images only; -- -m (--time-marker) - set custom full path or custom filename of time marker file (usage: `-m | --time-marker=`); -- -tmp (--tmp-path) - set custom directory full path for temporary files (usage: `-tmp | --tmp-path=`); -- -e (--exclude) - comma separated parts list of paths to files for exclusion from search (script removes from the search files in the full path of which includes any value from the list; usage: `-e | --exclude=`). +- -h (--help) - shows a help message; +- -v (--version) - shows a script version; +- -p (--path) - specify a full path to the input directory (usage: `-p | --path=`); +- -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 dependences; +- -t (--time) - sets the period for which to look for files by last modified time (usage: `-t | --time=`); +- -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 | --time-marker=`); +- -tmp (--tmp-path) - sets the custom directory full path for temporary files (usage: `-tmp | --tmp-path=`); +- -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 | --exclude=`). 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 options -p(--path) and -tmp(--tmp-path) may be with and without slash in the end of path; -- it is impossible to use together options -t(--time) and -n(--new-only); -- you must use option -m(--time-marker) with -n(--new-only) option. +- a 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. -Recommendation: use [GNU Screen](https://en.wikipedia.org/wiki/GNU_Screen) or analogs if there are many images in input directory, because optimization may can take a long time. +Recommendation: use [GNU Screen](https://en.wikipedia.org/wiki/GNU_Screen) or analogs if there are many images in an input directory, because the optimization may can take long time. ### Excluding folders/files from search ```bash @@ -76,7 +76,7 @@ Example: bash zImageOptimizer.sh -p /path/to/files -e /var/www/test.com,backup,uploads/orig.png ``` -### Usage with set period +### Usage with set the period ```bash bash zImageOptimizer.sh -p /path/to/files -t ``` @@ -91,26 +91,26 @@ Example: bash zImageOptimizer.sh -p /path/to/files -t 15d ``` -### Usage with time marker (recommended for cron usage) +### Usage with the time marker (recommended for a cron usage) ```bash bash zImageOptimizer.sh -p /path/to/files -n ``` -Notice: by default time marker file creates in working directory which set in -p(--path) option with filename **.timeMarker**. +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 option -m(--time-marker) and set new filename if you want to change time marker filename: +Use -m(--time-marker) option and set a new filename if you want to change the time marker filename: ```bash 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 option -m(--time-marker) and set new path and filename if you want to change time marker path: +Use -m(--time-marker) option and set a new path and filename if you want to change the time marker path: ```bash bash zImageOptimizer.sh -p /path/to/files -n -m /path/to/marker/directory/markerName ``` -Path to time marker will be `/path/to/marker/directory/markerName` +Path to the time marker will be `/path/to/marker/directory/markerName` #### Cron usage Using default time marker: @@ -131,15 +131,15 @@ Also you may collect all markers in own directory: 0 1 * * * /bin/bash zImageOptimizer.sh -p /second/directory -q -n -m /path/to/markers/directory/secondMarkerName ``` -Notice: use option -l(--less) if you want exclude optimizing process in cron email messages +Notice: use -l(--less) option if you want exclude optimizing process in cron email messages -#### Manually create/modify time marker file -You may manually create time marker file or change it last modified time: +#### Manually create/modify the time marker file +You may manually create the time marker file or change it last modified time: ```bash touch -m /path/to/marker/markerName ``` -If you want create marker with specify time or change marker last modified time with specify time: +If you want to create the marker with specify time or change marker last modified time with specify time: ```bash touch -t [[CC]YY]MMDDhhmm[.SS] ``` @@ -157,7 +157,7 @@ Example: touch -t 201712041426.00 /path/to/marker/markerName ``` -### Usage with custom path to temporary files directory +### Usage with custom path to the temporary files directory ```bash bash zImageOptimizer.sh -p /path/to/files -tmp /custom/path/to/temporary/directory ``` @@ -180,7 +180,7 @@ docker run -it -u "$UID:$GID" -d --volume /mnt/ImagesHundred/marker:/work/marker ## Automatical installing dependences Notice: curent user must be root or user with sudo access. -Start script in the optimization mode (-p|--path) or checking tools mode (-c|--check-only, recommended) if you want to install dependences automatically. It check installed tools and printing choise option dialog if one or more tools not found. Select option **Install dependences and exit** by typing appropriate number and press enter. Script install dependences based on your platform, distribution and package manager. You may need to enter password and confirm actions during installation dependences. After that restart script to recheck installed tools. +Start the script in the optimization mode (-p|--path) or the checking tools mode (-c|--check-only, recommended) if you want to install dependences automatically. It will check installed tools and printing a choise option dialog if one or more tools not found. Select **Install dependences and exit** option by typing a appropriate number and press enter. The script will install dependences based on your platform, distribution and package manager. You may need to enter a password and confirm actions during installation dependences. Restart script to recheck installed tools after that. Supported on: - DEB-based linux distributions (i686/amd64) @@ -324,23 +324,23 @@ rm -rf advancecomp-2.0 **I'm install dependences but one of tool is marked as NOT FOUND** -By default script looks for binary files into folowing directories /bin /usr/bin /usr/local/bin. If your binary file is not in these directories add your directory in variable BINARY_PATHS through a space like below and restart script +By default the script looks for binary files into folowing directories /bin /usr/bin /usr/local/bin. If your binary file is not in these directories add your directory in variable BINARY_PATHS through a space like below and restart the script ```bash BINARY_PATHS="/bin /usr/bin /usr/local/bin /your/custom/path" ``` **I have errors `djpeg: can't open /tmp/*` and `cjpeg: can't open /tmp/*` during optimization** -You have not write access to directory /tmp. Tools djpeg and cjpeg use this directory for temporary files. Use option -tmp(--tmp-path) for set custom path. +You have not write access to the directory /tmp. Tools djpeg and cjpeg use this directory for temporary files. Use -tmp(--tmp-path) option for set custom path. ## TODO -- [x] ~~add option for execute script without any questions and users actions (for cron usage)~~ +- [x] ~~add option for execute the script without any questions and users actions (for cron usage)~~ - [x] ~~add option for set time of the last change files for optimize only new images (for cron usage)~~ -- [ ] add option for set quality for more small files in output -- [x] ~~add option for check tools only~~ +- [ ] add an option for set quality for more small files in the output +- [x] ~~add an option for check tools only~~ - [x] ~~add support for optimize gif images~~ - [x] ~~add support for automatic install dependences on other platforms and distributions with other package managers~~ -- [ ] add support for automatic install dependences on other linux distributions +- [ ] add support for automatic install dependences on others linux distributions - [ ] add support for parallel optimization - [ ] even more to improve results of compression - [ ] add SVG support @@ -353,21 +353,24 @@ You have not write access to directory /tmp. Tools djpeg and cjpeg use this dire - telegram chat [@zImageOptimizer](https://t.me/zImageOptimizer) (RU) ## Reviews +- [sysadmin.pm](https://sysadmin.pm/zimageoptimizer/) (RU) - [glashkoff.com](https://glashkoff.com/blog/manual/kak-optimizirovat-izobrazheniya-sayta/) (RU) ## Donations -Do you like script? Would you like to support its development? Feel free to donate +Do you like the script? Would you like to support its development? Feel free to donate [![Donate](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://www.paypal.me/zevilz) ## Changelog -- 11.03.2018 - 0.9.3 - added restoration of original files if their size is less or equal than optimized files size -- 27.02.2018 - 0.9.2 - fixed PNG file permissions if script does not work from files owner +- 06.02.2018 - 0.9.5 - fixed quality in cjpeg/djpeg compression +- 28.10.2018 - 0.9.4 - [added check for empty temporary file after decompress images with djpeg](https://github.com/zevilz/zImageOptimizer/releases/tag/0.9.4) +- 11.03.2018 - 0.9.3 - added a restoration of original files if their size is less or equal than optimized files size +- 27.02.2018 - 0.9.2 - fixed PNG file permissions if the script does not work from files owner - 27.02.2018 - 0.9.1 - [added new features](https://github.com/zevilz/zImageOptimizer/releases/tag/0.9.1) and bugfixes -- 25.02.2018 - 0.9.0 - added support for working script on MacOS 10.10+ with automatic install dependences +- 25.02.2018 - 0.9.0 - added support for working the script on MacOS 10.10+ with automatic install dependences - 24.02.2018 - 0.8.1 - [added new parameter, updated info after optimizing, small fixes and small code refactoring](https://github.com/zevilz/zImageOptimizer/releases/tag/0.8.1) - 04.12.2017 - 0.8.0 - [new features, bugfixes and code refactoring](https://github.com/zevilz/zImageOptimizer/releases/tag/0.8.0) -- 30.11.2017 - 0.7.0 - added support for working script on FreeBSD with automatic install dependences, [bug fixes and more](https://github.com/zevilz/zImageOptimizer/releases/tag/0.7.0) +- 30.11.2017 - 0.7.0 - added support for working the script on FreeBSD with automatic install dependences, [bug fixes and more](https://github.com/zevilz/zImageOptimizer/releases/tag/0.7.0) - 28.11.2017 - 0.6.0 - added support for automatic install dependences on RHEL 6+ and Fedora 24+ - 25.11.2017 - 0.5.0 - bug fixes and code refactoring - 25.11.2017 - 0.4.0 - added support for automatic install dependences on Debian 7.* and some bugfixes diff --git a/zImageOptimizer.sh b/zImageOptimizer.sh index 49caaf2..a38463c 100644 --- a/zImageOptimizer.sh +++ b/zImageOptimizer.sh @@ -3,7 +3,7 @@ # URL: https://github.com/zevilz/zImageOptimizer # Author: Alexandr "zEvilz" Emshanov # License: MIT -# Version: 0.9.3 +# Version: 0.9.5 # Define default vars BINARY_PATHS="/bin /usr/bin /usr/local/bin" @@ -511,10 +511,25 @@ optimJpegtran() jpegtran -progressive -copy none -optimize "$1" > /dev/null } -optimMozjpeg() +optimXjpeg() { - djpeg -outfile "$TMP_PATH/$(basename "$1")" "$1" > /dev/null - cjpeg -optimize -progressive -outfile "$1" "$TMP_PATH/$(basename "$1")" > /dev/null + # decompress in temp file + djpeg -outfile "$TMP_PATH/$(basename "$1")" "$1" > /dev/null 2>/dev/null + + if [ -f "$TMP_PATH/$(basename "$1")" ]; then + + SIZE_CHECK=$(wc -c "$TMP_PATH/$(basename "$1")" | awk '{print $1}') + + if [[ SIZE_CHECK -gt 0 ]]; then + + # compress and replace original file if temp file exists and not empty + cjpeg -quality 100 -optimize -progressive -outfile "$1" "$TMP_PATH/$(basename "$1")" > /dev/null + + fi + + fi + + # cleanup if [ -f "$TMP_PATH/$(basename "$1")" ]; then rm "$TMP_PATH/$(basename "$1")" fi @@ -1027,7 +1042,7 @@ if ! [ -z "$IMAGES" ]; then fi if [[ $ISSET_djpeg -eq 1 && $ISSET_cjpeg -eq 1 ]]; then - optimMozjpeg "$IMAGE" + optimXjpeg "$IMAGE" fi elif [[ $EXT == "png" || $EXT == "PNG" ]]; then