├── LICENSE.md ├── README.md ├── install.sh └── resmushit-cli.sh /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 reSmush.it 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # reSmush.it Image Optimizer CLI client 2 | 3 | CLI client for the **reSmush.it Image Optimization API** 4 | 5 | 6 | ## What is reSmush.it ? 7 | 8 | Use reSmush.it Image Optimizer for **FREE** to **optimize your pictures file sizes**. Improve your performances by using reSmush.it, the 10+ billion images API optimizer. 9 | 10 | reSmush.it Image Optimizer allows you to use **free Image optimization** based on [reSmush.it API](https://resmush.it/ "Image Optimization API, developped by Charles Bourgeaux"). reSmush.it provides image size reduction based on several advanced algorithms. The API accept JPG, PNG and GIF files up to **5MB**. 11 | 12 | 13 | ## What is this tool ? 14 | 15 | This tool allow you to perform a powerful optimization of one or multiple pictures using a bash (command line) script based on the reSmush.it API. 16 | 17 | 18 | ## Getting Started 19 | 20 | This tool can optimize one or several pictures. The recursive option is not yet implemented. 21 | 22 | 1. Download the tool using this command `wget https://raw.githubusercontent.com/charlyie/resmushit-cli/master/install.sh | bash ./install.sh` 23 | 2. Make the file `resmushit-cli.sh` executable using the following command : `chmod +x resmushit-cli.sh` 24 | 3. Run the optimizer by typing command `./resmushit-cli.sh myfile.jpg` 25 | 26 | ### Prerequisites 27 | 28 | This will require to be executed on a Linux based Operating System (Linux or MacOS). The following packages must be installed on the system 29 | 30 | - `curl` 31 | - `jq` (*see [jq Homepage](https://stedolan.github.io/jq/ "jq is a lightweight and flexible command-line JSON processor.")*) 32 | 33 | ### Install on your system 34 | 35 | If you're using a Linux system (Debian, CentOS...) you can install after cloning from the GIT repo by typing the following command as `root` user (or using `sudo`) : 36 | 37 | - `mv resmushit-cli.sh /usr/bin/resmushit;chmod +x /usr/bin/resmushit` 38 | 39 | Your CLI optimizer will be available everywhere on your server/computer by typing `resmushit` 40 | 41 | ### Usage example 42 | *Replace in the examples below `resmushit` by `./resmushit-cli.sh` if you haven't installed the optimizer globally on your machine.* 43 | 44 | **Optimize a picture and create a new optimized picture file** 45 | 46 | `resmushit mypicture.jpg` 47 | 48 | *The optimized file will be named `mypicture-optimized.jpg`* 49 | 50 | **Optimize a picture and replace the original file** 51 | 52 | `resmushit mypicture.jpg --preserve-filename` 53 | 54 | **Optimize a picture and set a optimization factor** 55 | 56 | `resmushit -q 92 mypicture.jpg` 57 | 58 | **Optimize multiple pictures and export them into a folder** 59 | 60 | `resmushit --output myexportdirectory/ *` 61 | 62 | **Optimize a folder recursively ** 63 | 64 | `resmushit myfolder -r` 65 | `resmushit /path/to/folder -r` 66 | `resmushit * -r` 67 | 68 | **Optimize a picture and preserve its EXIF data** 69 | 70 | `resmushit mypicture.jpg --preserve-exif` 71 | 72 | 73 | ### Options 74 | 75 | - `-h` or `--help` : display the help menu 76 | - `-v` or `--version` : display the current version of reSmushit CLI client 77 | - `-q ` or `--quality ` : specify the quality factor between 0 and 100 (default is 92). 78 | - `-o ` or `--output ` : specify an output directory (will be created if not present) 79 | - `-r` or `--recursive` : execute the optimization to the folder specified recusively 80 | - `--preserve-filename` : avoid to add `-optimized` in the filename when the image is optimized 81 | - `--preserve-exif` : will preserve EXIF data in the file after optimization 82 | - `--notime` : avoid to display timer in output 83 | - `--quiet` : run in quiet mode 84 | - `--update` : perform an auto-upgrade process 85 | 86 | 87 | ## Bugs 88 | 89 | All bugs should be reported to : [hello@resmush.it](mailto:hello@resmush.it) 90 | 91 | 92 | ## Roadmap 93 | 94 | - [X] Add "preserve filename" option 95 | - [X] Add EXIF preservation option 96 | - [ ] Add progress bar mode 97 | - [X] Add support of recursive optimization 98 | - [X] Add an installer mode 99 | - [X] Add an auto-update method based on Git repo 100 | - [X] Avoid sending to API if file is > 5MB 101 | 102 | 103 | ## Versioning 104 | 105 | We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/charlyie/resmushit-wordpress). 106 | 107 | 108 | ## Authors 109 | 110 | * **Charles Bourgeaux** - *Initial work* - [reSmush.it](https://resmush.it) 111 | 112 | 113 | ## License 114 | 115 | This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details 116 | 117 | 118 | ## Support us on our new platform 119 | Support us through Ko-Fi ! 120 | 121 | [![Kofi](https://feed.resmush.it/images/kofi-button.png)](https://ko-fi.com/resmushit) 122 | 123 | 124 | ## Changelog 125 | 126 | ### v1.1.1 (build 20210501) 127 | 128 | - Avoid errors if SSL certificate is down 129 | - Add user agent in requests 130 | 131 | ### v1.1.0 (build 20201115) 132 | 133 | - Add recursive mode for complete folder optimization 134 | 135 | ### v1.0.9 (build 20200216) 136 | 137 | - Add an installer mode 138 | 139 | ### v1.0.8 (build 20190122) 140 | 141 | - Fix an issue for filenames with spaces 142 | - Better check for the update temporary file writeability 143 | 144 | ### v1.0.7 (build 20181222) 145 | 146 | - Better update check and upgrade minor fix 147 | 148 | ### v1.0.6 (build 20181222) 149 | 150 | - Check if file is > 5MB before sending it to the API 151 | 152 | ### v1.0.5 (build 20180819) 153 | 154 | - Minor update bugfix 155 | 156 | ### v1.0.4 (build 20180819) 157 | 158 | - Add an update verification 159 | - Add an auto-upgrade process 160 | 161 | ### v1.0.3 (build 20180818) 162 | 163 | - Adding EXIF preservation support 164 | - Add a verification is file specified exists 165 | - Correction for the filename non-preservation by default 166 | 167 | 168 | ### v1.0.2 (build 20180813) 169 | 170 | - Add a "preserve-filename" option 171 | - Preserve original filename when exporting in another directory 172 | 173 | 174 | ### v1.0.1 (build 20180812) 175 | 176 | - Minor correction, unused variable, filename correction 177 | 178 | 179 | ### v1.0.0 (build 20180812) 180 | 181 | - Initial version of the tool 182 | - Support 1 or multiple file optimization. 183 | - Avoid downloading picture if not optimized 184 | - No recursive mode implemented yet 185 | - Auto-exclude files which aren't supported 186 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | git clone --depth 1 https://github.com/charlyie/resmushit-cli.git 4 | chmod +x resmushit-cli/resmushit-cli.sh 5 | rm $0 6 | -------------------------------------------------------------------------------- /resmushit-cli.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # reSmush.it CLI (Shell client) 3 | # chmod 755 resmushit.sh 4 | # cp resmushit.sh /usr/bin/resmushit 5 | # then run: ./resmushit 6 | # 7 | # A CLI for reSmush.it, the Image Optimizer API 8 | # 9 | # The MIT License (MIT) 10 | # Copyright (c) 2018 Charles Bourgeaux and contributors 11 | # You are not obligated to bundle the LICENSE file with your projects as long 12 | # as you leave these references intact in the header comments of your source files. 13 | 14 | VERSION="1.1.1" 15 | BUILD_DATE="20210501" 16 | REQUIRED_PACKAGES=( "curl" "jq" ) 17 | 18 | # System variables 19 | API_URL="http://api.resmush.it" 20 | QUALITY=92 21 | OUTPUT_DIR="." 22 | PRESERVE_EXIF=false 23 | PRESERVE_FILENAME=false 24 | RECURSIVE_BROWSING=false 25 | APP_DIR=$(dirname "$0") 26 | TIME_LOG=true 27 | QUIET_MODE=false 28 | RED="\033[0;31m" 29 | GREEN="\033[0;32m" 30 | LBLUE="\033[0;36m" 31 | NC="\033[0m" # No Color 32 | POSITIONAL=() 33 | UPDATE_LOCKFILE="/tmp/.resmushit-cli.update" 34 | MAXFILESIZE=5242880 #5Mb 35 | 36 | # Display output and save it to log file. 37 | cli_output(){ 38 | if [[ $QUIET_MODE == "true" ]]; 39 | then 40 | return 41 | fi 42 | TIME="[`date '+%Y-%m-%d %H:%M:%S'`] " 43 | COLOR_OPEN_TAG='' 44 | COLOR_CLOSE_TAG=$NC 45 | if [[ $2 == "green" ]]; 46 | then 47 | COLOR_OPEN_TAG=$GREEN 48 | elif [[ $2 == "red" ]]; 49 | then 50 | COLOR_OPEN_TAG=$RED 51 | elif [[ $2 == "blue" ]]; 52 | then 53 | COLOR_OPEN_TAG=$LBLUE 54 | elif [[ $2 == "standard" ]]; 55 | then 56 | COLOR_OPEN_TAG=$NC 57 | fi 58 | if [[ $3 == "notime" ]] || [[ $TIME_LOG == false ]]; then 59 | TIME="" 60 | fi 61 | printf "${COLOR_OPEN_TAG}${TIME}$1 ${COLOR_CLOSE_TAG}\n" 62 | } 63 | 64 | check_update(){ 65 | _SCRIPT_NAME=`basename "$0"` 66 | 67 | # Perform update verification once a day 68 | if [ -f ${UPDATE_LOCKFILE} ]; then 69 | _UPDATE_LOCKFILE_VALUE=`cat $UPDATE_LOCKFILE` 70 | 71 | if [[ $_UPDATE_LOCKFILE_VALUE == "false" ]]; then 72 | if [[ $(find "${UPDATE_LOCKFILE}" -mtime -1 -print) ]]; then 73 | return 74 | fi 75 | else 76 | cli_output "An update is available. Run \`${_SCRIPT_NAME} --update\` to perform an upgrade" blue notime 77 | return 78 | fi 79 | 80 | fi 81 | cli_output "Checking for update..." standard notime 82 | _REQUEST_OUTPUT=`curl --silent "https://api.github.com/repos/charlyie/resmushit-cli/tags"` 83 | _REMOTE_VERSION=`echo ${_REQUEST_OUTPUT} | jq -r '.[0].name'` 84 | _TARBALL=`echo ${_REQUEST_OUTPUT} | jq -r '.[0].tarball_url'` 85 | 86 | if [[ $_REMOTE_VERSION == "v${VERSION}" ]]; then 87 | cli_output "No update required (remote version is : ${_REMOTE_VERSION})" green notime 88 | if [ -f "${UPDATE_LOCKFILE}" ]; then 89 | if [ -w $UPDATE_LOCKFILE ]; then 90 | echo "false" > $UPDATE_LOCKFILE 91 | else 92 | cli_output "Cannot write temporary file $UPDATE_LOCKFILE, please check if this file is writeable" red notime 93 | fi 94 | else 95 | echo "false" > $UPDATE_LOCKFILE 96 | fi 97 | else 98 | _INSTALL_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 99 | _SCRIPT_PATH=`echo ${_INSTALL_DIR}/${_SCRIPT_NAME}` 100 | 101 | cli_output "An update is available. Run \`${_SCRIPT_NAME} --update\` to perform an upgrade" blue notime 102 | if [ -f "${UPDATE_LOCKFILE}" ]; then 103 | if [ -w $UPDATE_LOCKFILE ]; then 104 | echo "true" > $UPDATE_LOCKFILE 105 | else 106 | cli_output "Cannot write temporary file $UPDATE_LOCKFILE, please check if this file is writeable" red notime 107 | fi 108 | else 109 | echo "true" > $UPDATE_LOCKFILE 110 | fi 111 | fi 112 | } 113 | 114 | do_update(){ 115 | _REQUEST_OUTPUT=`curl --silent "https://api.github.com/repos/charlyie/resmushit-cli/tags"` 116 | _REMOTE_VERSION=`echo ${_REQUEST_OUTPUT} | jq -r '.[0].name'` 117 | _TARBALL=`echo ${_REQUEST_OUTPUT} | jq -r '.[0].tarball_url'` 118 | 119 | if [[ $_REQUEST_OUTPUT == "v${VERSION}" ]]; then 120 | cli_output "No update required (remote version is : ${_REMOTE_VERSION})" green 121 | else 122 | _INSTALL_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 123 | _SCRIPT_NAME=`basename "$0"` 124 | _SCRIPT_PATH=`echo ${_INSTALL_DIR}/${_SCRIPT_NAME}` 125 | 126 | cli_output "> Local version : v${VERSION}" standard notime 127 | cli_output "> Remote version : ${_REMOTE_VERSION}" standard notime 128 | 129 | if [[ "v${VERSION}" != "${_REMOTE_VERSION}" ]]; then 130 | cli_output "An update is available. Launching upgrade..." blue notime 131 | if [ ! -w "$_SCRIPT_PATH" ]; then 132 | cli_output "Current executable not writable. Please run with sudo." red 133 | exit 0 134 | fi 135 | 136 | cli_output "> Downloading from ${_TARBALL}..." standard notime 137 | if [ -d "/tmp/resmushit-last-release" ]; then 138 | rm -rf /tmp/resmushit-last-release 139 | fi 140 | mkdir -p /tmp/resmushit-last-release 141 | curl -L ${_TARBALL} --output /tmp/resmushit-cli-last-release.tar.gz --silent 142 | cli_output "> Extracting tarball..." standard notime 143 | tar xf /tmp/resmushit-cli-last-release.tar.gz -C /tmp/resmushit-last-release 144 | cli_output "> Replacing executable..." standard notime 145 | cp /tmp/resmushit-last-release/*/resmushit-cli.sh $_SCRIPT_PATH 146 | rm -f $UPDATE_LOCKFILE 147 | cli_output "> New installed version is :" green notime 148 | $_SCRIPT_PATH --version 149 | exit 0 150 | else 151 | cli_output "No update available" blue notime 152 | fi 153 | fi 154 | } 155 | 156 | optimize_file(){ 157 | current_file=$1 158 | cli_output "Processing : $current_file" 159 | # Extract file data 160 | filefolder=$(dirname -- "$current_file") 161 | filename=$(basename -- "$current_file") 162 | extension="${filename##*.}" 163 | current_file_lower=$( echo $current_file | tr '[:upper:]' '[:lower:]') 164 | filename="${filename%.*}" 165 | if [[ $PRESERVE_FILENAME == true ]] || [[ $OUTPUT_DIR != "." ]]; then 166 | output_filename="${filename}.${extension}" 167 | else 168 | output_filename="${filename}-optimized.${extension}" 169 | fi 170 | 171 | 172 | # Optimize only authorized extensions 173 | if [[ $current_file_lower =~ \.(png|jpg|jpeg|gif|bmp|tif|tiff) ]]; 174 | then 175 | if [ ! -f "${current_file}" ]; then 176 | cli_output "File ${current_file} not found" red 177 | else 178 | filesize=`wc -c "${current_file}" | awk '{print $1}'` 179 | if [[ $filesize -lt $MAXFILESIZE ]]; then 180 | cli_output "Sending picture ${current_file} to api..." 181 | api_output=$(curl -k -F "files=@${current_file}" --silent ${API_URL}"/?qlty=${QUALITY}&exif=${PRESERVE_EXIF}" -A "resmushit-cli/$VERSION") 182 | api_error=$(echo ${api_output} | jq .error) 183 | 184 | # Check if the API returned an error 185 | if [[ "$api_error" != 'null' ]]; 186 | then 187 | api_error_long=$(echo ${api_output} | jq -r .error_long) 188 | cli_output "API responds Error #${api_error} : ${api_error_long}" 189 | exit 0 190 | else 191 | # Display result and download optimized file 192 | api_percent=$(echo ${api_output} | jq .percent) 193 | if [[ $api_percent == 0 ]]; 194 | then 195 | cli_output "File already optimized. No downloading necessary" green 196 | else 197 | api_src_size=$(echo ${api_output} | jq .src_size | awk '{ split( "B KB MB GB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f%s", $1, v[s] }') 198 | api_dest_size=$(echo ${api_output} | jq -r .dest_size | awk '{ split( "B KB MB GB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f%s", $1, v[s] }') 199 | cli_output "File optimized by ${api_percent}%% (from ${api_src_size} to ${api_dest_size}). Retrieving..." green 200 | api_file_output=$(echo ${api_output} | jq -r .dest) 201 | curl -k ${api_file_output} --output "${filefolder}/${output_filename}" --silent -A "resmushit-cli/$VERSION" 202 | cli_output "File saved as ${filefolder}/${output_filename}" green 203 | fi 204 | fi 205 | else 206 | cli_output "File ${current_file} is beyond 5MB (${filesize} bytes), skipping" blue 207 | fi 208 | fi 209 | else 210 | cli_output "File ${current_file} is not an allowed picture format, skipping" blue 211 | fi 212 | } 213 | export -f optimize_file 214 | export -f cli_output 215 | 216 | 217 | # Manage arguments 218 | while [[ $# -gt 0 ]] 219 | do 220 | key="$1" 221 | 222 | case $key in 223 | -q|--quality) 224 | QUALITY="$2" 225 | shift # past argument 226 | shift # past value 227 | ;; 228 | --notime) 229 | TIME_LOG=false 230 | shift # past argument 231 | ;; 232 | --quiet) 233 | QUIET_MODE=true 234 | shift # past argument 235 | ;; 236 | -o|--output) 237 | OUTPUT_DIR="$2" 238 | shift # past argument 239 | shift # past value 240 | ;; 241 | --preserve-exif) 242 | PRESERVE_EXIF=true 243 | shift # past argument 244 | ;; 245 | --preserve-filename) 246 | PRESERVE_FILENAME=true 247 | shift # past argument 248 | ;; 249 | -r|--recursive) 250 | RECURSIVE_BROWSING=true 251 | shift # past argument 252 | ;; 253 | --update) 254 | shift # past argument 255 | do_update 256 | exit 0 257 | ;; 258 | -h|--help) 259 | shift # past argument 260 | cli_output "reSmush.it Image Optimizer CLI client v.${VERSION}, a Command Line Interface for reSmush.it, the Image Optimizer API" green notime 261 | cli_output "(c) reSmush.it - Charles Bourgeaux \n" green notime 262 | cli_output "Usage: ./resmushit-cli.sh [--quality ] [--output or --output \t specify an output directory." standard notime 266 | cli_output " -q or --quality \t\t specify the quality factor between 0 and 100 (default is 92)." standard notime 267 | cli_output " -r or --recursive\t\t\t\t browse files recursively in the specified path." standard notime 268 | cli_output " -h or --help \t\t\t\t\t print this help." standard notime 269 | cli_output " -v or --version \t\t\t\t display the version of reSmushit CLI client." standard notime 270 | cli_output " --notime \t\t\t\t\t avoid display timer in output." standard notime 271 | cli_output " --preserve-exif \t\t\t\t will preserve EXIF data while optimizing." standard notime 272 | cli_output " --preserve-filename \t\t\t\t avoid appending '-optimized' in the filename." standard notime 273 | cli_output " --quiet \t\t\t\t\t avoid output display." standard notime 274 | cli_output " --update \t\t\t\t\t perform an upgrade of this app.\n" standard notime 275 | exit 0 276 | ;; 277 | -v|--version) 278 | shift # past argument 279 | cli_output "reSmush.it CLI v.${VERSION} (build ${BUILD_DATE})" standard notime 280 | exit 0 281 | ;; 282 | -*) # unknown option 283 | cli_output "Invalid option: ${1}. Type --help to show help" red notime 284 | shift 285 | exit 0 286 | ;; 287 | --*) # unknown option 288 | cli_output "Invalid option: ${1}. Type --help to show help" red notime 289 | shift 290 | exit 0 291 | ;; 292 | *) # unknown option 293 | FILES+=("$1") # save it in an array for later 294 | shift # past argument 295 | ;; 296 | esac 297 | done 298 | set -- "${FILES[@]}" # restore positional parameters 299 | 300 | 301 | 302 | # Check required packages and try to install them 303 | for x in ${REQUIRED_PACKAGES[@]} 304 | do 305 | if ! which $x > /dev/null; 306 | then 307 | cli_output "Missing package $x. " red 308 | echo -e "Try to install it ? (y/n) \c" 309 | read 310 | if [[ "$REPLY" == "y" ]]; 311 | then 312 | # Package installation on MacOS platform 313 | if [ "$(uname)" == "Darwin" ]; 314 | then 315 | cli_output "Trying to install the package using homebrew..." 316 | brew install $x 317 | elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; 318 | then 319 | if [[ `id -u` -ne 0 ]]; 320 | then 321 | cli_output "Package installation needs ROOT privileges. Please log as root or use sudo." 322 | exit 0 323 | fi 324 | 325 | if [ -n "$(command -v yum)" ]; 326 | then 327 | sudo yum update 328 | sudo yum install $x 329 | if ! which sudo > /dev/null || ! which apt-get > /dev/null; 330 | then 331 | cli_output "Cannot install package '$x' automatically. Please install it manually." 332 | exit 0 333 | fi 334 | elif [ -n "$(command -v apt-get)" ]; 335 | then 336 | sudo apt-get -qq update 337 | sudo apt-get -y -qq install $x 338 | if ! which sudo > /dev/null || ! which apt-get > /dev/null; 339 | then 340 | cli_output "Cannot install package '$x' automatically. Please install it manually." 341 | exit 0 342 | fi 343 | else 344 | cli_output "Unsupported Linux package manager. Try to install the package $x manually." 345 | exit 0 346 | fi 347 | else 348 | cli_output "Unsupported platform. Try to install the package $x manually." 349 | exit 0 350 | fi 351 | 352 | else 353 | cli_output "Some package are missing. Try to install them before." 354 | exit 0 355 | fi 356 | fi 357 | done 358 | 359 | if [ ${#FILES[@]} -eq 0 ]; 360 | then 361 | cli_output "No input file specified. Usage: resmushit-cli.sh " red notime 362 | exit 0 363 | fi 364 | 365 | # Check if an update is available 366 | check_update 367 | 368 | # On first launch create a configuration, otherwise, read from it 369 | api_test=$(curl -k --write-out %{http_code} --silent --output /dev/null --connect-timeout 5 ${API_URL}) 370 | if [[ "$api_test" == "000" ]]; 371 | then 372 | cli_output "reSmush.it API is unreachable" 373 | exit 0 374 | fi 375 | 376 | # Creates an output directory if needed 377 | if [ ! -d "$OUTPUT_DIR" ]; 378 | then 379 | cli_output "Output directory ${OUTPUT_DIR} isn't existing. Creating..." blue 380 | mkdir -p ${OUTPUT_DIR} 381 | fi 382 | 383 | cli_output "Initializing images optimization with quality factor : ${QUALITY}%%" blue 384 | 385 | for current_file in "${FILES[@]}" 386 | do 387 | if [ -d "${current_file}" ]; then 388 | if [[ $RECURSIVE_BROWSING == true ]] ; then 389 | OIFS="$IFS" 390 | IFS=$'\n' 391 | for f in $(find ${current_file} -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.gif' -o -iname '*.tif' -o -iname '*.tiff' -o -iname '*.bmp' \) ) 392 | do 393 | optimize_file "$f" 394 | done 395 | IFS="$OIFS" 396 | fi 397 | else 398 | optimize_file "$current_file" 399 | fi 400 | done 401 | cli_output "Optimization completed" green --------------------------------------------------------------------------------