Skip to content

Instantly share code, notes, and snippets.

@svx
Forked from caubry/imageCompression.sh
Created December 13, 2023 14:50
Show Gist options
  • Save svx/4c558030b4cb2fd38a707c14effe2ab6 to your computer and use it in GitHub Desktop.
Save svx/4c558030b4cb2fd38a707c14effe2ab6 to your computer and use it in GitHub Desktop.

Revisions

  1. @caubry caubry revised this gist Mar 17, 2014. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -169,6 +169,7 @@ function run_jpegoptim_quality()
    outputSize=$(du -h "$imageOutput" | awk '{print $1}');
    echo -e "${yellow}\n\xF0\x9f\x8d\xba Compression done! \nOutput: $outputSize, $imageOutput${textreset} \n";
    remove_temp_folder;

    else
    outputSize=$(du -h "$filePath" | awk '{print $1}');
    outputSize=$(du -h "${originalName}$extension" | awk '{print $1}');
    @@ -364,6 +365,14 @@ function run_batch()
    create_batch_directory;
    batch=true;

    while true; do
    read -p "`echo -e '\n**********************************************************************************'` `echo -e '\n************** Please enter the maximum image quality factor [0-'$qualityCompression'] **************'``echo -e '\n** Note that if you do not enter a value, the default value of' $qualityCompression 'will be used. **'``echo -e '\n**********************************************************************************'` $cr" userQualityCompression;
    case $userQualityCompression in
    [0-$qualityCompression]*) qualityCompression=$userQualityCompression; break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done

    let totalOriginalFileOutput=0;

    while read -r files;
    @@ -707,4 +716,4 @@ then
    echo "Compression works with PNG, JPG and JPEG only.";
    exit;
    fi
    fi
    fi
  2. @caubry caubry revised this gist Feb 28, 2014. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ currentDir="${filePath%/*}/";
    originalName="${filePath%.*}";

    # Extension to be added after the compression
    customExt="_MINI";
    customExt="";

    # Supported image extensions (with or without capitalisation)
    pngExt="png";
    @@ -396,7 +396,7 @@ function run_batch()
    then
    run_jpegoptim;
    fi
    done < <(find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \))
    done < <(find "$currentDir" -maxdepth 1 \( ! -regex '.*/\..*' \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \) \))

    let totalNewFileOutput=0;
    let totalOriginalFileOutput=0;
    @@ -406,7 +406,7 @@ function run_batch()
    do
    newFiles+=("$line");
    originalFiles="${line##*/}";
    originalFiles="${originalFiles%_MINI*}";
    originalFiles="${originalFiles%$customExt*}";

    while read -r originalLines;
    do
    @@ -420,7 +420,7 @@ function run_batch()
    newFileOutput=${newFileOutput%?};
    newFileOutput=${newFileOutput%.*};
    let totalNewFileOutput=$totalNewFileOutput+$newFileOutput;
    done < <(find "$batchFolder" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \))
    done < <(find "$batchFolder" -maxdepth 1 \( ! -regex '.*/\..*' \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \) \))

    let totalSpaceSaved=$totalOriginalFileOutput-$totalNewFileOutput;

    @@ -432,7 +432,7 @@ function run_batch()
    newFiles=$i;
    echo "$newFiles"
    newFilesName="${newFiles##*/}";
    newFilesName="${newFilesName%_MINI*}";
    newFilesName="${newFilesName%$customExt*}";
    newFilesList+=("$newFilesName");
    done

    @@ -482,7 +482,7 @@ function run_batch()
    exit;
    }

    itemsCount=($(find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \) | grep -v ^l | wc -l));
    itemsCount=($(find "$currentDir" -maxdepth 1 \( ! -regex '.*/\..*' \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \) \) | grep -v ^l | wc -l));

    if [ "$itemsCount" -ge "2" ]
    then
    @@ -493,7 +493,7 @@ then
    echo "$files";
    originalFileList+=("$files");
    originalFileListCopy+=("$files");
    done < <(find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \))
    done < <(find "$currentDir" -maxdepth 1 \( ! -regex '.*/\..*' \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \) \))

    while true; do
    read -p "`echo $'\n> '`Do you want to compress these $itemsCount images? (Y/N): " yn;
    @@ -707,4 +707,4 @@ then
    echo "Compression works with PNG, JPG and JPEG only.";
    exit;
    fi
    fi
    fi
  3. @caubry caubry revised this gist Feb 23, 2014. No changes.
  4. @caubry caubry revised this gist Feb 23, 2014. No changes.
  5. @caubry caubry revised this gist Feb 20, 2014. 1 changed file with 73 additions and 26 deletions.
    99 changes: 73 additions & 26 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -1,69 +1,107 @@
    #!bin/bash
    pwd=`pwd`

    cr=`echo -e '\n> '`;
    textreset=$(tput sgr0);
    yellow=$(tput setaf 2);
    red=$(tput setaf 1);
    # Set the quality factor to 80
    qualityCompression=80;
    # Set a minimum quality factor to 20
    qualityLimit=20;
    # To run a new compression
    second_run_compression=false;
    # For batch compression
    batch=false;

    # Colours for the script
    yellow=$(tput setaf 2);
    red=$(tput setaf 1);
    # Reset colours
    textreset=$(tput sgr0);

    # Return line, with ">"
    cr=`echo -e '\n> '`;

    # Ask path to user
    echo -n "Please enter the file path: $cr";
    # Save path entered by user
    read filePath;

    # Check if path exists
    if [[ ! -f "$filePath" ]]
    then
    # Exit script if path doesn't exist
    echo "File "$filePath" not found!";
    exit;
    fi

    # Grab file directory
    currentDir="${filePath%/*}/";
    # Grab file path and name, without the extension
    originalName="${filePath%.*}";

    # Extension to be added after the compression
    customExt="_MINI";

    # Supported image extensions (with or without capitalisation)
    pngExt="png";
    jpgExt="jpg";
    jpegExt="jpeg";

    # Create a batch directory, where compressed images can be saved to
    function create_batch_directory()
    {
    # Grab the batch compression folder
    batchFolder="${currentDir}batch_compression/";

    # Check if the folder exists
    if [[ ! -d "$batchFolder" ]]
    then
    # Create folder if it doesn't exist
    mkdir "$batchFolder";
    else
    # Remove old batch compression folder and create a new one
    # if the folder exists
    rm -rf "$batchFolder";
    mkdir "$batchFolder";
    fi
    }

    # Remove temporary folder
    function remove_temp_folder()
    {
    # Check if temporary folder exists
    if [[ -d "$tempDir" ]]
    then
    # Remove the directory
    find -f "temp" \( ! -iname "*.DS_Store" \) | xargs rm -rf
    fi
    }

    # Remove the directory
    function make_image_copy()
    {
    # Store temp directory
    tempDir="${pwd}/temp/";

    # Check if temporary folder doesn't exists
    if [[ ! -d "$tempDir" ]]
    then
    # Create directory
    mkdir "$tempDir";
    fi

    # If user has chosen to run a batch
    if $batch
    then
    name="${files##*/}";
    name="${name%.*}";

    cp "$files" "${tempDir}${name}${fileExtension}";
    tempFilePath="${tempDir}${name}${fileExtension}";
    if [[ "$imageType" == *$pngExt* ]]
    then
    cp "$files" "${tempDir}${name}";
    tempFilePath="${tempDir}${name}";
    else
    cp "$files" "${tempDir}${name}${fileExtension}";
    tempFilePath="${tempDir}${name}${fileExtension}";
    fi
    else
    cp "$filePath" "${tempDir}_temp";
    tempFilePath="${tempDir}_temp";
    @@ -99,6 +137,7 @@ function run_jpegoptim_quality()
    then
    echo -e "\n${red}******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n You should decrease the image quality factor to decrease the size of the file. \n********************************************************************************${textreset} \n";
    remove_temp_folder;
    # rm "$imageOutput";

    if ! $batch
    then
    @@ -132,7 +171,8 @@ function run_jpegoptim_quality()
    remove_temp_folder;
    else
    outputSize=$(du -h "$filePath" | awk '{print $1}');
    echo -e "${yellow}\n\xF0\x9f\x8d\xba Compression done! \nOutput: $outputSize, $filePath${textreset} \n";
    outputSize=$(du -h "${originalName}$extension" | awk '{print $1}');
    echo -e "${yellow}\n\xF0\x9f\x8d\xba Compression done! \nOutput: $outputSize, "${originalName}$extension"${textreset} \n";
    remove_temp_folder;
    exit;
    fi
    @@ -215,8 +255,8 @@ function run_jpegoptim()
    then
    while true; do
    twenty_kb=20;
    currentSize=$(du -h "$filePath" | awk '{print $1}');
    currentSize=${currentSize%?};
    currentSize=$(ls -l "$filePath" | awk '{print $5}');
    currentSize=$((currentSize/1000));
    currentSize=$((currentSize-3));
    read -p "`echo -e '\n**********************************************************************************'` `echo -e '\n************** Please enter the minimum image size in Kb [0-'$currentSize'] **************'``echo -e '\n** Note that if you do not enter a value, the default value of' $twenty_kb 'will be used. **'``echo -e '\n**********************************************************************************'` $cr" userSizeInput;

    @@ -251,7 +291,7 @@ function run_pngquant()
    while true; do
    read -p "Do you wish to install this program (Y/N)? " yn
    case $yn in
    [Yy]* ) install_pngquant;;
    [Yy]* ) libpng;;
    [Nn]* ) echo "End"; exit;;
    * ) echo "Please answer Yes or No.";;
    esac
    @@ -261,19 +301,13 @@ function run_pngquant()

    if $batch
    then

    make_image_copy;
    imageOutput="${batchFolder}${name}${fileExtension}";
    fileOutput=$(ls -l "$imageOutput" | awk '{print $5}');

    fileOutput=$(ls -l "$tempFilePath" | awk '{print $5}');
    pngquant -vf "$tempFilePath" --ext "$fileExtension";
    mv "${tempFilePath}$fileExtension" "$batchFolder";

    imageOutput="${tempFilePath}$fileExtension";

    currentCompressedFile="${filesNoExt}$fileExtension";
    mv "$currentCompressedFile" "$batchFolder";
    imageOutput="${batchFolder}${fileNameNoExt}$fileExtension";
    batchNoExt="${tempFilePath%.*}";
    currentImage="${batchNoExt}${fileExtension}";
    mv "${currentImage}" "$batchFolder";
    imageOutput="${batchFolder}${currentImage##*/}";
    else
    while true; do
    read -p "`echo $'\n> '`Do you wish to overwrite your image (Y/N)? " yn
    @@ -314,14 +348,13 @@ function run_pngquant()
    then
    if $batch
    then
    echo "rm temp"
    remove_temp_folder;
    else
    cp "$imageOutput" "$filePath";
    output=$(du -h "$filePath");
    remove_temp_folder;
    fi
    fi

    echo -e "${yellow}\n\xF0\x9f\x8d\xba Compression done! \nOutput: $output${textreset} \n";
    fi
    }
    @@ -377,8 +410,8 @@ function run_batch()

    while read -r originalLines;
    do
    originalFileOutput=$(du -h "$originalLines" | awk '{print $1}');
    originalFileOutput=${originalFileOutput%?};
    originalFileOutput=$(ls -l "$originalLines" | awk '{print $5}');
    originalFileOutput=$((originalFileOutput / 1000));
    originalFileOutput=${originalFileOutput%.*};
    let totalOriginalFileOutput=$totalOriginalFileOutput+$originalFileOutput;
    done < <(find "$currentDir" -maxdepth 1 \( -iname "*$originalFiles*" \))
    @@ -388,7 +421,7 @@ function run_batch()
    newFileOutput=${newFileOutput%.*};
    let totalNewFileOutput=$totalNewFileOutput+$newFileOutput;
    done < <(find "$batchFolder" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \))

    let totalSpaceSaved=$totalOriginalFileOutput-$totalNewFileOutput;

    echo -e "\n${yellow}********************** Congratulation! You have saved "$totalSpaceSaved"K! **********************${textreset} \n";
    @@ -472,6 +505,20 @@ then
    done
    fi

    function libpng()
    {
    cd /usr/local/include/;

    if [[ -f 'libpng15' ]]
    then
    install_pngquant;
    else
    brew install libpng
    pause 'Press [Enter] key to install pngquant...';
    install_pngquant;
    fi
    }

    function install_pngquant()
    {
    cd ~/Documents/;
  6. @caubry caubry revised this gist Jan 21, 2014. 1 changed file with 194 additions and 50 deletions.
    244 changes: 194 additions & 50 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@
    pwd=`pwd`

    cr=`echo -e '\n> '`;
    textreset=$(tput sgr0);
    yellow=$(tput setaf 2);
    red=$(tput setaf 1);
    qualityCompression=80;
    qualityLimit=20;
    second_run_compression=false;
    @@ -26,31 +29,55 @@ jpegExt="jpeg";

    function create_batch_directory()
    {
    batchCompression="batch_compression";
    batchFolder="${currentDir}batch_compression/";

    if [[ ! -d "${currentDir}$batchCompression/" ]]; then
    mkdir "${currentDir}$batchCompression/";
    if [[ ! -d "$batchFolder" ]]
    then
    mkdir "$batchFolder";
    else
    rm -rf "$batchFolder";
    mkdir "$batchFolder";
    fi

    batchFolder="${currentDir}$batchCompression/";
    }

    function remove_temp_folder()
    {
    if [[ -d "$tempDir" ]]; then
    if [[ -d "$tempDir" ]]
    then
    find -f "temp" \( ! -iname "*.DS_Store" \) | xargs rm -rf
    fi
    }

    function make_image_copy()
    {
    tempDir="${pwd}/temp/";

    if [[ ! -d "$tempDir" ]]
    then
    mkdir "$tempDir";
    fi

    if $batch
    then
    name="${files##*/}";
    name="${name%.*}";

    cp "$files" "${tempDir}${name}${fileExtension}";
    tempFilePath="${tempDir}${name}${fileExtension}";
    else
    cp "$filePath" "${tempDir}_temp";
    tempFilePath="${tempDir}_temp";
    fi
    }

    function run_jpegoptim_quality()
    {
    if $batch
    then
    cp "$files" "${filesNoExt}${fileExtension}";
    currentCompressedFile="${filesNoExt}${fileExtension}";
    jpegoptim -v --max=$qualityCompression "$currentCompressedFile";
    mv "$currentCompressedFile" "$batchFolder";
    imageOutput="${batchFolder}${fileNameNoExt}$fileExtension";
    make_image_copy;
    jpegoptim -v --max=$qualityCompression "$tempFilePath";
    mv "$tempFilePath" "$batchFolder";
    imageOutput="${batchFolder}${name}${fileExtension}";
    fileOutput=$(ls -l "$imageOutput" | awk '{print $5}');
    else
    if $overwrite
    @@ -68,9 +95,9 @@ function run_jpegoptim_quality()
    fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');
    fi

    if [[ "$fileOutput" -ge $((originalLsSize-1000)) ]]
    if [[ "$fileOutput" -ge $originalLsSize ]]
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n You should decrease the image quality factor to decrease the size of the file. \n******************************************************************************** \n";
    echo -e "\n${red}******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n You should decrease the image quality factor to decrease the size of the file. \n********************************************************************************${textreset} \n";
    remove_temp_folder;

    if ! $batch
    @@ -90,16 +117,25 @@ function run_jpegoptim_quality()
    read -p "$adjustImageQuality" yn
    case $yn in
    [Yy]* ) run_jpegoptim_command; break;;
    [Nn]* ) break;;
    [Nn]* ) exit;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    else
    rm "$imageOutput";
    fi
    else
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \n";
    remove_temp_folder;
    if $batch
    then
    outputSize=$(du -h "$imageOutput" | awk '{print $1}');
    echo -e "${yellow}\n\xF0\x9f\x8d\xba Compression done! \nOutput: $outputSize, $imageOutput${textreset} \n";
    remove_temp_folder;
    else
    outputSize=$(du -h "$filePath" | awk '{print $1}');
    echo -e "${yellow}\n\xF0\x9f\x8d\xba Compression done! \nOutput: $outputSize, $filePath${textreset} \n";
    remove_temp_folder;
    exit;
    fi
    fi
    }

    @@ -134,7 +170,7 @@ function run_jpegoptim_command()
    done
    fi
    else
    jpegoptim -v --max=$qualityCompression "$filePath";
    # jpegoptim -v --max=$qualityCompression "$filePath";
    outputSize=$(ls -l "$filePath" | awk '{print $5}');
    run_jpegoptim_twenty_kb;
    fi
    @@ -158,7 +194,7 @@ function run_jpegoptim()
    exit;
    }

    twenty_kb=20000;
    twenty_kb=20;

    if $batch
    then
    @@ -167,13 +203,33 @@ function run_jpegoptim()
    if [[ $originalLsSize -ge $twenty_kb ]]
    then
    while true; do
    read -p "`echo $'\n> '`Do you need this image to be under 20kb (Y/N): " yn;
    read -p "`echo $'\n> '`Do you need this image to be under a certain size (Y/N)? " yn;
    case $yn in
    [Yy]* ) once=false; run_jpegoptim_command; break;;
    [Nn]* ) once=true; run_jpegoptim_command; break;;
    [Yy]* ) once=false; break;;
    [Nn]* ) once=true; run_jpegoptim_command;;
    * ) echo "Please answer Yes or No.";;
    esac
    done

    if ! $once
    then
    while true; do
    twenty_kb=20;
    currentSize=$(du -h "$filePath" | awk '{print $1}');
    currentSize=${currentSize%?};
    currentSize=$((currentSize-3));
    read -p "`echo -e '\n**********************************************************************************'` `echo -e '\n************** Please enter the minimum image size in Kb [0-'$currentSize'] **************'``echo -e '\n** Note that if you do not enter a value, the default value of' $twenty_kb 'will be used. **'``echo -e '\n**********************************************************************************'` $cr" userSizeInput;

    if (( 0 <= $userSizeInput && $userSizeInput <= $currentSize ))
    then
    twenty_kb=$((userSizeInput*1000));
    run_jpegoptim_command;
    else
    twenty_kb=$((twenty_kb*1000));
    run_jpegoptim_command;
    fi
    done
    fi
    else
    once=true;
    run_jpegoptim_command;
    @@ -186,21 +242,6 @@ function pause()
    read -p "$*";
    }

    function make_image_copy()
    {
    temp="temp";

    if [[ ! -d "${pwd}/$temp/" ]]; then
    mkdir "${pwd}/$temp/";
    fi

    tempName="_temp";
    tempDir="${pwd}/$temp/";

    cp "$filePath" "${tempDir}$tempName";
    tempFilePath=${tempDir}$tempName;
    }

    function run_pngquant()
    {
    command -v pngquant >/dev/null 2>&1 ||
    @@ -220,7 +261,16 @@ function run_pngquant()

    if $batch
    then
    pngquant -vf "$files" --ext "$fileExtension";

    make_image_copy;
    imageOutput="${batchFolder}${name}${fileExtension}";
    fileOutput=$(ls -l "$imageOutput" | awk '{print $5}');

    pngquant -vf "$tempFilePath" --ext "$fileExtension";
    mv "${tempFilePath}$fileExtension" "$batchFolder";

    imageOutput="${tempFilePath}$fileExtension";

    currentCompressedFile="${filesNoExt}$fileExtension";
    mv "$currentCompressedFile" "$batchFolder";
    imageOutput="${batchFolder}${fileNameNoExt}$fileExtension";
    @@ -245,32 +295,34 @@ function run_pngquant()
    fi
    fi

    output=$(du -h "$imageOutput");
    output=$(du -h "$imageOutput");
    outputSizeFile=$(ls -l "$imageOutput" | awk '{print $5}');

    if [ $outputSizeFile -ge $originalLsSize ]
    then
    if $batch
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! ********************** \n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n******************************************************************************** \n";
    echo -e "\n${red}******************************************************************************** \n********************** No compression has been performed! ********************** \n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n********************************************************************************${textreset} \n";
    rm "$imageOutput";
    else
    remove_temp_folder;
    echo -e "\n******************************************************************************** \n********** The compression has already been performed on this file. ************ \n******************************************************************************** \n";
    rm "$imageOutput";
    echo -e "\n${red}******************************************************************************** \n********** The compression has already been performed on this file. ************ \n******************************************************************************** \n";
    fi
    else
    if $overwrite
    then
    if $batch
    then
    echo "rm temp"
    remove_temp_folder;
    else
    cp "$imageOutput" "$filePath";
    remove_temp_folder;
    fi
    fi

    echo -e "\n\xF0\x9f\x8d\xba Compression done! \nOutput: $output \n";
    echo -e "${yellow}\n\xF0\x9f\x8d\xba Compression done! \nOutput: $output${textreset} \n";
    fi
    }

    @@ -279,7 +331,9 @@ function run_batch()
    create_batch_directory;
    batch=true;

    find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \) | while read files;
    let totalOriginalFileOutput=0;

    while read -r files;
    do
    imageType="${files##*.}";
    filesNoExt="${files%.*}";
    @@ -309,15 +363,104 @@ function run_batch()
    then
    run_jpegoptim;
    fi
    done < <(find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \))

    let totalNewFileOutput=0;
    let totalOriginalFileOutput=0;
    let totalSpaceSaved=0;

    while read -r line;
    do
    newFiles+=("$line");
    originalFiles="${line##*/}";
    originalFiles="${originalFiles%_MINI*}";

    while read -r originalLines;
    do
    originalFileOutput=$(du -h "$originalLines" | awk '{print $1}');
    originalFileOutput=${originalFileOutput%?};
    originalFileOutput=${originalFileOutput%.*};
    let totalOriginalFileOutput=$totalOriginalFileOutput+$originalFileOutput;
    done < <(find "$currentDir" -maxdepth 1 \( -iname "*$originalFiles*" \))

    newFileOutput=$(du -h "$line" | awk '{print $1}');
    newFileOutput=${newFileOutput%?};
    newFileOutput=${newFileOutput%.*};
    let totalNewFileOutput=$totalNewFileOutput+$newFileOutput;
    done < <(find "$batchFolder" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \))

    let totalSpaceSaved=$totalOriginalFileOutput-$totalNewFileOutput;

    echo -e "\n${yellow}********************** Congratulation! You have saved "$totalSpaceSaved"K! **********************${textreset} \n";
    echo -e "The new images are: \n";

    for i in "${newFiles[@]}"
    do
    newFiles=$i;
    echo "$newFiles"
    newFilesName="${newFiles##*/}";
    newFilesName="${newFilesName%_MINI*}";
    newFilesList+=("$newFilesName");
    done

    for y in "${newFilesList[@]}"
    do
    while read -r files;
    do
    matchingFiles+=("$files");
    done < <(find "$currentDir" -maxdepth 1 \( -iname "*$y*" \))
    done

    for z in "${originalFileList[@]}"
    do
    for k in "${matchingFiles[@]}"
    do
    if [[ "$z" = "$k" ]]
    then
    for (( i = 0; i < ${#originalFileListCopy[@]}; i++ ))
    do
    if [ "${originalFileListCopy[$i]}" = "${z}" ]
    then
    unset "originalFileList[$i]";
    fi
    done
    fi
    done
    done

    if [ "${#originalFileList[@]}" -gt "0" ]
    then
    if [ "${#originalFileList[@]}" -eq "1" ]
    then
    imageString="image"
    else
    imageString="images"
    fi

    echo -e "\n******************************************************************************** \n\n${red}Error occurred with the following $imageString:${textreset} \n";

    for f in "${originalFileList[@]}"
    do
    echo -e "${red}"$f"${textreset}";
    done

    echo -e "\n${red}Scroll up for more details.${textreset} \n";
    fi
    exit;
    }

    itemsCount=($(find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \) | grep -v ^l | wc -l));

    if [ "$itemsCount" -ge "2" ]
    then
    echo -e "\n******************************************************************************** \n********************** This directory contains $itemsCount images! ***********************\n******************************************************************************** \n";
    find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \);

    while read -r files;
    do
    echo "$files";
    originalFileList+=("$files");
    originalFileListCopy+=("$files");
    done < <(find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \))

    while true; do
    read -p "`echo $'\n> '`Do you want to compress these $itemsCount images? (Y/N): " yn;
    @@ -423,7 +566,7 @@ function run_jpegoptim_twenty_kb()
    copyTempFilePath="${tempFilePath}$copy_ext";
    cp "$tempFilePath" "$copyTempFilePath";

    while [ $twenty_kb -le $outputSize -a $qualityCompression -gt $qualityLimit ]
    while [[ $twenty_kb -le $outputSize && $qualityCompression -gt $qualityLimit ]]
    do
    cp "$copyTempFilePath" "$tempFilePath";
    qualityCompression=$((qualityCompression-1));
    @@ -446,13 +589,14 @@ function run_jpegoptim_twenty_kb()

    if [[ "$fileOutput" -ge "$originalLsSize" ]]
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n**** The minimum image quality factor of 20 might has already been reached. **** \n************* It is not adviced to go below this quality factor. *************** \n******************************************************************************** \n";
    echo -e "\n${red}******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n**** The minimum image quality factor of 20 might has already been reached. **** \n************* It is not adviced to go below this quality factor. *************** \n********************************************************************************${textreset} \n";
    elif [ $twenty_kb -le $outputSize -a $qualityCompression -gt 0 ]
    then
    echo -e "\n******************************************************************************** \n*********************** Compression has been performed! ************************ \n******************************************************************************** \nThe minimum quality factor has been set to 20 to avoid the image to be corrupted \n*********** Note that the size of the new file is over as 20Kb! ************ \n******************************************************************************** \n\n\xF0\x9f\x8d\xba Output: ${originalName}$extension, $outputSize \n";
    echo -e "\n${yellow}******************************************************************************** \n*********************** Compression has been performed! ************************ \n******************************************************************************** \nThe minimum quality factor has been set to 20 to avoid the image to be corrupted \n*********** Note that the size of the new file is over as 20Kb! ************ \n******************************************************************************** \n\n\xF0\x9f\x8d\xba Output: ${originalName}$extension, $outputSize${textreset} \n";

    while true; do
    read -p "`echo $'\n> '`Do you still want your file to be below 20Kb? (Y/N): " yn;
    userImageSize=$((twenty_kb/1000));
    read -p "`echo $'\n> '`Do you still want your file to be below $userImageSize Kb? (Y/N): " yn;
    case $yn in
    [Yy]* ) qualityLimit=0; run_jpegoptim_twenty_kb; break;;
    [Nn]* ) break;;
    @@ -471,7 +615,7 @@ function run_jpegoptim_twenty_kb()
    esac
    done
    else
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \n\nOutput: ${originalName}$extension, $outputSize \n";
    echo -e "${yellow}\n\xF0\x9f\x8d\xba Compression done! \n\nOutput: ${originalName}$extension, $outputSize${textreset} \n";
    if [ $qualityCompression -eq 0 ]
    then
    echo -e "******************************************************************************** \n*************** The image quality factor of 0 has been reached. **************** \n****************** This size is the lowest one you can get. ******************** \n******************************************************************************** \n";
    @@ -506,11 +650,11 @@ if ! $batch
    then
    if [[ "$imageType" == *$pngExt* ]]
    then
    run_pngquant;
    run_pngquant;
    exit;
    elif [[ "$imageType" == *$jpgExt* ]] || [[ "$imageType" == *$jpegExt* ]]
    then
    run_jpegoptim;
    run_jpegoptim;
    exit;
    else
    echo "Compression works with PNG, JPG and JPEG only.";
  7. @caubry caubry revised this gist Dec 12, 2013. 1 changed file with 359 additions and 183 deletions.
    542 changes: 359 additions & 183 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    #!bin/bash
    pwd=`pwd`

    # Need to add batch option
    # If more than one file is located in the current folder of the file
    # Give this option
    # Compress png, jpeg and jpg and add them into a folder called 'batch compression'
    cr=`echo -e '\n> '`;
    qualityCompression=80;
    qualityLimit=20;
    second_run_compression=false;
    batch=false;

    echo -n "Please enter the file path: ";
    echo -n "Please enter the file path: $cr";
    read filePath;

    if [[ ! -f "$filePath" ]]
    @@ -15,49 +16,192 @@ then
    exit;
    fi


    imageType="${filePath##*.}";
    lengthPath="${#filePath}";
    currentDir="${filePath%/*}/";
    originalName="${filePath%.*}";

    originalDuSizeFile=$(du -h "$filePath" | awk '{print $1}');
    originalLsSize=$(ls -l "$filePath" | awk '{print $5}');
    originalDuSize=${originalDuSizeFile%?};
    customExt="_MINI";
    pngExt="png";
    jpgExt="jpg";
    jpegExt="jpeg";

    pause()
    function create_batch_directory()
    {
    read -p "$*";
    batchCompression="batch_compression";

    if [[ ! -d "${currentDir}$batchCompression/" ]]; then
    mkdir "${currentDir}$batchCompression/";
    fi

    batchFolder="${currentDir}$batchCompression/";
    }

    install_pngquant()
    function remove_temp_folder()
    {
    cd ~/Documents/;
    git clone git://github.com/pornel/pngquant.git;
    if [[ -d "$tempDir" ]]; then
    find -f "temp" \( ! -iname "*.DS_Store" \) | xargs rm -rf
    fi
    }

    if [[ -d 'pngquant' ]]
    function run_jpegoptim_quality()
    {
    if $batch
    then
    cd pngquant;
    make -s;
    make -s install;
    cd ..;
    rm -rf ~/Documents/pngquant;
    cp "$files" "${filesNoExt}${fileExtension}";
    currentCompressedFile="${filesNoExt}${fileExtension}";
    jpegoptim -v --max=$qualityCompression "$currentCompressedFile";
    mv "$currentCompressedFile" "$batchFolder";
    imageOutput="${batchFolder}${fileNameNoExt}$fileExtension";
    fileOutput=$(ls -l "$imageOutput" | awk '{print $5}');
    else
    if $overwrite
    then
    currentFile=$filePath;
    extension=".$imageType";
    else
    make_image_copy;
    currentFile=$tempFilePath;
    extension=$fileExtension;
    fi

    jpegoptim -v --max=$qualityCompression "$currentFile";
    mv "$currentFile" "${originalName}$extension";
    fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');
    fi

    command -v pngquant >/dev/null 2>&1 ||
    {
    echo "Something went horribly wrong and pngquant hasn't been installed! (╯°□°)╯︵ ┻━┻";
    exit;
    }
    if [[ "$fileOutput" -ge $((originalLsSize-1000)) ]]
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n You should decrease the image quality factor to decrease the size of the file. \n******************************************************************************** \n";
    remove_temp_folder;

    echo -e "********************************** \n******* Installation done! ******* \n********************************** \n";
    pause 'Press [Enter] key to compress your image...';
    run_pngquant;
    if ! $batch
    then
    qualityCompression=$((qualityCompression-1));

    if [[ "$qualityCompression" -gt 0 ]]
    then
    adjustImageQuality="Do you wish to adjust the image quality factor below $qualityCompression? (Y/N): "
    elif [[ "$qualityCompression" -eq 0 ]]
    then
    adjustImageQuality="Do you wish to adjust the image quality factor at $qualityCompression? (Y/N): "
    else
    exit;
    fi
    while true; do
    read -p "$adjustImageQuality" yn
    case $yn in
    [Yy]* ) run_jpegoptim_command; break;;
    [Nn]* ) break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    else
    rm "$imageOutput";
    fi
    else
    echo "Couldn't clone pngquant git repo. \nDo you have an Internet access?";
    exit;
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \n";
    remove_temp_folder;
    fi
    }

    function run_jpegoptim_command()
    {
    if $batch
    then
    run_jpegoptim_quality;
    else
    if ! $second_run_compression
    then
    while true; do
    read -p "`echo $'\n> '`Do you wish to overwrite your image (Y/N)? " yn
    case $yn in
    [Yy]* ) second_run_compression=true; overwrite=true; break;;
    [Nn]* ) second_run_compression=true; overwrite=false; break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    fi

    if $once
    then
    if [[ "$qualityCompression" -gt -1 ]]
    then
    while true; do
    read -p "`echo -e '\n**********************************************************************************'` `echo -e '\n************** Please enter the maximum image quality factor [0-'$qualityCompression'] **************'``echo -e '\n** Note that if you do not enter a value, the default value of' $qualityCompression 'will be used. **'``echo -e '\n**********************************************************************************'` $cr" userQualityCompression;
    case $userQualityCompression in
    [0-$qualityCompression]*) qualityCompression=$userQualityCompression ; run_jpegoptim_quality;;
    * ) run_jpegoptim_quality;;
    esac
    done
    fi
    else
    jpegoptim -v --max=$qualityCompression "$filePath";
    outputSize=$(ls -l "$filePath" | awk '{print $5}');
    run_jpegoptim_twenty_kb;
    fi
    fi
    }

    function run_jpegoptim()
    {
    command -v jpegoptim >/dev/null 2>&1 ||
    {
    echo -e >&2 "Jpegoptim hasn't been installed.";

    while true; do
    read -p "`echo $'\n> '`Do you wish to install this program (Y/N)? " yn
    case $yn in
    [Yy]* ) install_libjpeg;;
    [Nn]* ) echo "End"; exit;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    exit;
    }

    twenty_kb=20000;

    if $batch
    then
    run_jpegoptim_command;
    else
    if [[ $originalLsSize -ge $twenty_kb ]]
    then
    while true; do
    read -p "`echo $'\n> '`Do you need this image to be under 20kb (Y/N): " yn;
    case $yn in
    [Yy]* ) once=false; run_jpegoptim_command; break;;
    [Nn]* ) once=true; run_jpegoptim_command; break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    else
    once=true;
    run_jpegoptim_command;
    fi
    fi
    }

    function pause()
    {
    read -p "$*";
    }

    function make_image_copy()
    {
    temp="temp";

    if [[ ! -d "${pwd}/$temp/" ]]; then
    mkdir "${pwd}/$temp/";
    fi

    tempName="_temp";
    tempDir="${pwd}/$temp/";

    cp "$filePath" "${tempDir}$tempName";
    tempFilePath=${tempDir}$tempName;
    }

    run_pngquant()
    function run_pngquant()
    {
    command -v pngquant >/dev/null 2>&1 ||
    {
    @@ -74,23 +218,146 @@ run_pngquant()
    exit;
    }

    pngquant -vf "$filePath" --ext "$fileExtension";
    imageOutput="${originalName}$fileExtension";
    output=$(du -h "$imageOutput");
    outputSizeFile=$(du -h "$imageOutput" | awk '{print $1}');
    outputSize=${outputSizeFile%?};
    if $batch
    then
    pngquant -vf "$files" --ext "$fileExtension";
    currentCompressedFile="${filesNoExt}$fileExtension";
    mv "$currentCompressedFile" "$batchFolder";
    imageOutput="${batchFolder}${fileNameNoExt}$fileExtension";
    else
    while true; do
    read -p "`echo $'\n> '`Do you wish to overwrite your image (Y/N)? " yn
    case $yn in
    [Yy]* ) overwrite=true; break;;
    [Nn]* ) overwrite=false; break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done

    if $overwrite
    then
    make_image_copy;
    pngquant -vf "$tempFilePath" --ext "$fileExtension";
    imageOutput="${tempFilePath}$fileExtension";
    else
    pngquant -vf "$filePath" --ext "$fileExtension";
    imageOutput="${originalName}$fileExtension";
    fi
    fi

    output=$(du -h "$imageOutput");
    outputSizeFile=$(ls -l "$imageOutput" | awk '{print $5}');

    if [[ $outputSize -ge $originalDuSize ]]
    if [ $outputSizeFile -ge $originalLsSize ]
    then
    echo -e "\n******************************************************************************** \n********** The compression has already been performed on this file. ************ \n******************************************************************************** \n";
    exit;
    if $batch
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! ********************** \n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n******************************************************************************** \n";
    rm "$imageOutput";
    else
    remove_temp_folder;
    echo -e "\n******************************************************************************** \n********** The compression has already been performed on this file. ************ \n******************************************************************************** \n";
    fi
    else
    if $overwrite
    then
    if $batch
    then
    remove_temp_folder;
    else
    cp "$imageOutput" "$filePath";
    remove_temp_folder;
    fi
    fi

    echo -e "\n\xF0\x9f\x8d\xba Compression done! \nOutput: $output \n";
    fi
    }

    function run_batch()
    {
    create_batch_directory;
    batch=true;

    find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \) | while read files;
    do
    imageType="${files##*.}";
    filesNoExt="${files%.*}";
    fileNameNoExt="${filesNoExt##*/}"

    case ${imageType:(-1)} in
    [A-Z]*) upperLetter=true;;
    [a-z]*) upperLetter=false;;
    esac

    if $upperLetter
    then
    fileExtension="${customExt}".$imageType"";
    lowerCaseLetters=$(echo $imageType | awk '{print tolower($0)}');
    imageType="$lowerCaseLetters";
    else
    imageType="$imageType";
    fileExtension="${customExt}".$imageType"";
    fi

    originalLsSize=$(ls -l "$files" | awk '{print $5}');

    if [[ "$imageType" == *$pngExt* ]]
    then
    run_pngquant;
    elif [[ "$imageType" == *$jpgExt* ]] || [[ "$imageType" == *$jpegExt* ]]
    then
    run_jpegoptim;
    fi
    done
    }

    itemsCount=($(find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \) | grep -v ^l | wc -l));

    if [ "$itemsCount" -ge "2" ]
    then
    echo -e "\n******************************************************************************** \n********************** This directory contains $itemsCount images! ***********************\n******************************************************************************** \n";
    find "$currentDir" -maxdepth 1 \( -iregex '.*\(jpg\)' -o -iregex '.*\(png\)' -o -iregex '.*\(jpeg\)' \);

    while true; do
    read -p "`echo $'\n> '`Do you want to compress these $itemsCount images? (Y/N): " yn;
    case $yn in
    [Yy]* ) run_batch; break;;
    [Nn]* ) break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    fi

    function install_pngquant()
    {
    cd ~/Documents/;
    git clone git://github.com/pornel/pngquant.git;

    if [[ -d 'pngquant' ]]
    then
    cd pngquant;
    make -s;
    make -s install;
    cd ..;
    rm -rf ~/Documents/pngquant;

    command -v pngquant >/dev/null 2>&1 ||
    {
    echo "Something went horribly wrong and pngquant hasn't been installed! (╯°□°)╯︵ ┻━┻";
    exit;
    }

    echo -e "********************************** \n******* Installation done! ******* \n********************************** \n";
    pause 'Press [Enter] key to compress your image...';
    run_pngquant;
    else
    echo "Couldn't clone pngquant git repo. \nDo you have an Internet access?";
    exit;
    fi
    }

    install_libjpeg()
    function install_libjpeg()
    {
    cd /usr/local/include/;

    @@ -120,7 +387,7 @@ install_libjpeg()
    fi
    }

    install_jpegoptim()
    function install_jpegoptim()
    {
    cd ~/Documents/;
    git clone https://github.com/tjko/jpegoptim.git;
    @@ -149,32 +416,16 @@ install_jpegoptim()
    fi
    }

    make_image_copy()
    {
    temp="temp/";

    if [[ ! -d ${pwd}$temp ]]; then
    mkdir ${pwd}$temp;
    fi

    tempName="_temp";
    tempDir=${pwd}$temp;
    cp "$filePath" ${tempDir}$tempName;
    tempFilePath=${tempDir}$tempName;
    }

    run_jpegoptim_twenty_kb()
    function run_jpegoptim_twenty_kb()
    {
    make_image_copy;

    twenty_kb=20000;
    copy_ext="_copy";
    copyTempFilePath="${tempFilePath}$copy_ext";
    cp $tempFilePath $copyTempFilePath;
    cp "$tempFilePath" "$copyTempFilePath";

    while [ $twenty_kb -le $outputSize -a $qualityCompression -gt 20 ]
    while [ $twenty_kb -le $outputSize -a $qualityCompression -gt $qualityLimit ]
    do
    cp $copyTempFilePath $tempFilePath;
    cp "$copyTempFilePath" "$tempFilePath";
    qualityCompression=$((qualityCompression-1));
    jpegoptim -v --max=$qualityCompression "$tempFilePath";
    outputSize=$(ls -l "$tempFilePath" | awk '{print $5}');
    @@ -187,132 +438,54 @@ run_jpegoptim_twenty_kb()
    extension=$fileExtension;
    fi

    rm $copyTempFilePath;
    mv $tempFilePath "${originalName}$extension";
    fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');

    if [[ "$fileOutput" -ge "$originalLsSize" ]]
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n**** The minimum image quality factor of 20 might has already been reached. **** \n************* It is not adviced to go below this quality factor. *************** \n******************************************************************************** \n";

    if ! $overwrite; then
    rm "${originalName}$extension";
    fi

    remove_temp_folder;
    exit;
    else
    if [[ $twenty_kb -le $outputSize ]]; then
    echo -e "\n******************************************************************************** \n*********************** Compression has been performed! ************************ \n******************************************************************************** \nThe minimum quality factor has been set to 20 to avoid the image to be corrupted \n*********** Note that the size of the new file is superior as 20Kb! ************ \n******************************************************************************** \n\n\xF0\x9f\x8d\xba Output: ${originalName}$extension, $outputSize \n";
    else
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \nOutput: ${originalName}$extension, $outputSize \n";
    fi
    remove_temp_folder;
    exit;
    fi
    }

    run_jpegoptim_quality()
    {
    if $overwrite
    then
    currentFile=$filePath;
    extension=".$imageType";
    else
    make_image_copy;
    currentFile=$tempFilePath;
    extension=$fileExtension;
    if [[ -d "$tempDir" ]]; then
    rm "$copyTempFilePath";
    mv "$tempFilePath" "${originalName}$extension";
    fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');
    fi

    echo $currentFile

    jpegoptim -v --max=$qualityCompression "$currentFile";
    mv $currentFile "${originalName}$extension";
    fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');

    if [[ "$fileOutput" -ge "$originalLsSize" ]]
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n You should decrease the image quality factor to decrease the size of the file. \n******************************************************************************** \n";

    if ! $overwrite; then
    rm "${originalName}$extension";
    fi

    remove_temp_folder;
    exit;
    else
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \n";
    remove_temp_folder;
    exit;
    fi
    }

    remove_temp_folder()
    {
    if [[ -d ${pwd}$temp ]]; then
    find -f ${pwd}$temp \( ! -iname "*.DS_Store" \) | xargs rm -rf
    fi
    }

    run_jpegoptim_command()
    {
    while true; do
    read -p "Do you wish to overwrite your image (Y/N)? " yn
    case $yn in
    [Yy]* ) overwrite=true; break;;
    [Nn]* ) overwrite=false; break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done

    if $once
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n**** The minimum image quality factor of 20 might has already been reached. **** \n************* It is not adviced to go below this quality factor. *************** \n******************************************************************************** \n";
    elif [ $twenty_kb -le $outputSize -a $qualityCompression -gt 0 ]
    then
    echo -e "\n******************************************************************************** \n*********************** Compression has been performed! ************************ \n******************************************************************************** \nThe minimum quality factor has been set to 20 to avoid the image to be corrupted \n*********** Note that the size of the new file is over as 20Kb! ************ \n******************************************************************************** \n\n\xF0\x9f\x8d\xba Output: ${originalName}$extension, $outputSize \n";

    while true; do
    read -p "Please enter the maximum image quality factor [0-100]: " qualityCompression;
    case $qualityCompression in
    [0-99]*) run_jpegoptim_quality;;
    * ) echo "$qualityCompression, Not a number between 0 and 100.";;
    read -p "`echo $'\n> '`Do you still want your file to be below 20Kb? (Y/N): " yn;
    case $yn in
    [Yy]* ) qualityLimit=0; run_jpegoptim_twenty_kb; break;;
    [Nn]* ) break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    else
    qualityCompression=80;
    jpegoptim -v --max=$qualityCompression "$filePath";
    outputSize=$(ls -l "$filePath" | awk '{print $5}');
    run_jpegoptim_twenty_kb;
    fi
    }

    run_jpegoptim()
    {
    command -v jpegoptim >/dev/null 2>&1 ||
    {
    echo -e >&2 "Jpegoptim hasn't been installed.";

    qualityCompression=19

    while true; do
    read -p "Do you wish to install this program (Y/N)? " yn
    read -p "`echo $'\n> '`Do you wish to manually set the image quality factor below $qualityCompression? (Y/N): " yn;
    case $yn in
    [Yy]* ) install_libjpeg;;
    [Nn]* ) echo "End"; exit;;
    [Yy]* ) once=true; run_jpegoptim_command; break;;
    [Nn]* ) break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    exit;
    }

    while true; do
    read -p "Do you need this image to be under 20kb (Y/N): " yn;
    case $yn in
    [Yy]* ) once=false; run_jpegoptim_command $once; break;;
    [Nn]* ) once=true; run_jpegoptim_command $once; break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    else
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \n\nOutput: ${originalName}$extension, $outputSize \n";
    if [ $qualityCompression -eq 0 ]
    then
    echo -e "******************************************************************************** \n*************** The image quality factor of 0 has been reached. **************** \n****************** This size is the lowest one you can get. ******************** \n******************************************************************************** \n";
    fi
    fi
    remove_temp_folder;
    exit;
    }

    customExt="_MINI";
    pngExt="png";
    jpgExt="jpg";
    jpegExt="jpeg";
    imageType="${filePath##*.}";

    originalDuSizeFile=$(du -h "$filePath" | awk '{print $1}');
    originalLsSize=$(ls -l "$filePath" | awk '{print $5}');
    originalDuSize=${originalDuSizeFile%?};

    case ${imageType:(-1)} in
    [A-Z]*) upperLetter=true;;
    @@ -329,15 +502,18 @@ else
    fileExtension="${customExt}".$imageType"";
    fi

    if [[ "$imageType" == *$pngExt* ]]
    if ! $batch
    then
    run_pngquant;
    exit;
    elif [[ "$imageType" == *$jpgExt* ]] || [[ "$imageType" == *$jpegExt* ]]
    then
    run_jpegoptim;
    exit;
    else
    echo "Compression works with PNG, JPG and JPEG only.";
    exit;
    if [[ "$imageType" == *$pngExt* ]]
    then
    run_pngquant;
    exit;
    elif [[ "$imageType" == *$jpgExt* ]] || [[ "$imageType" == *$jpegExt* ]]
    then
    run_jpegoptim;
    exit;
    else
    echo "Compression works with PNG, JPG and JPEG only.";
    exit;
    fi
    fi
  8. @caubry caubry revised this gist Dec 10, 2013. 1 changed file with 38 additions and 19 deletions.
    57 changes: 38 additions & 19 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,21 @@
    #!bin/bash
    pwd=`pwd`

    # Need to add batch option
    # If more than one file is located in the current folder of the file
    # Give this option
    # Compress png, jpeg and jpg and add them into a folder called 'batch compression'

    echo -n "Please enter the file path: ";
    read filePath;

    if [ ! -f "$filePath" ]
    if [[ ! -f "$filePath" ]]
    then
    echo "File "$filePath" not found!";
    exit;
    fi


    imageType="${filePath##*.}";
    lengthPath="${#filePath}";
    originalName="${filePath%.*}";
    @@ -143,6 +149,20 @@ install_jpegoptim()
    fi
    }

    make_image_copy()
    {
    temp="temp/";

    if [[ ! -d ${pwd}$temp ]]; then
    mkdir ${pwd}$temp;
    fi

    tempName="_temp";
    tempDir=${pwd}$temp;
    cp "$filePath" ${tempDir}$tempName;
    tempFilePath=${tempDir}$tempName;
    }

    run_jpegoptim_twenty_kb()
    {
    make_image_copy;
    @@ -168,12 +188,17 @@ run_jpegoptim_twenty_kb()
    fi

    rm $copyTempFilePath;
    mv $tempFilePath ${originalName}$extension;
    mv $tempFilePath "${originalName}$extension";
    fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');

    if [[ "$fileOutput" -ge "$originalLsSize" ]]
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n**** The minimum image quality factor of 20 might has already been reached. **** \n************* It is not adviced to go below this quality factor. *************** \n******************************************************************************** \n";

    if ! $overwrite; then
    rm "${originalName}$extension";
    fi

    remove_temp_folder;
    exit;
    else
    @@ -198,20 +223,22 @@ run_jpegoptim_quality()
    currentFile=$tempFilePath;
    extension=$fileExtension;
    fi

    echo $currentFile

    jpegoptim -v --max=$qualityCompression "$currentFile";
    mv $currentFile ${originalName}$extension;
    mv $currentFile "${originalName}$extension";
    fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');

    if [[ "$fileOutput" -ge "$originalLsSize" ]]
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n You should decrease the image quality factor to decrease the size of the file. \n******************************************************************************** \n";
    remove_temp_folder;

    case $overwrite in
    (false) rm ${originalName}$extension;;
    esac
    if ! $overwrite; then
    rm "${originalName}$extension";
    fi

    remove_temp_folder;
    exit;
    else
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \n";
    @@ -220,19 +247,11 @@ run_jpegoptim_quality()
    fi
    }

    make_image_copy()
    {
    temp="temp/";
    mkdir ${pwd}$temp;
    tempName="_temp";
    tempDir=${pwd}$temp;
    cp $filePath ${tempDir}$tempName;
    tempFilePath=${tempDir}$tempName;
    }

    remove_temp_folder()
    {
    rm -rf ${pwd}$temp;
    if [[ -d ${pwd}$temp ]]; then
    find -f ${pwd}$temp \( ! -iname "*.DS_Store" \) | xargs rm -rf
    fi
    }

    run_jpegoptim_command()
    @@ -321,4 +340,4 @@ then
    else
    echo "Compression works with PNG, JPG and JPEG only.";
    exit;
    fi
    fi
  9. @caubry caubry revised this gist Dec 9, 2013. 1 changed file with 60 additions and 20 deletions.
    80 changes: 60 additions & 20 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,11 @@ fi

    imageType="${filePath##*.}";
    lengthPath="${#filePath}";
    originalName="${filePath:0:($lengthPath-4)}";
    originalName="${filePath%.*}";

    originalDuSizeFile=$(du -h "$filePath" | awk '{print $1}');
    originalLsSize=$(ls -l "$filePath" | awk '{print $5}');
    originalDuSize=${originalDuSizeFile%?};

    pause()
    {
    @@ -66,9 +70,18 @@ run_pngquant()

    pngquant -vf "$filePath" --ext "$fileExtension";
    imageOutput="${originalName}$fileExtension";
    outputSize=$(du -h "$imageOutput");
    echo -e "\xF0\x9f\x8d\xba Compression done! \nOutput: $outputSize";
    exit;
    output=$(du -h "$imageOutput");
    outputSizeFile=$(du -h "$imageOutput" | awk '{print $1}');
    outputSize=${outputSizeFile%?};

    if [[ $outputSize -ge $originalDuSize ]]
    then
    echo -e "\n******************************************************************************** \n********** The compression has already been performed on this file. ************ \n******************************************************************************** \n";
    exit;
    else
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \nOutput: $output \n";
    exit;
    fi
    }

    install_libjpeg()
    @@ -136,7 +149,7 @@ run_jpegoptim_twenty_kb()

    twenty_kb=20000;
    copy_ext="_copy";
    copyTempFilePath=${tempFilePath}$copy_ext;
    copyTempFilePath="${tempFilePath}$copy_ext";
    cp $tempFilePath $copyTempFilePath;

    while [ $twenty_kb -le $outputSize -a $qualityCompression -gt 20 ]
    @@ -149,42 +162,69 @@ run_jpegoptim_twenty_kb()

    if $overwrite
    then
    extension=$fileExtension;
    extension=".$imageType";
    else
    extension=$imageType;
    extension=$fileExtension;
    fi

    rm $copyTempFilePath;
    mv $tempFilePath ${originalName}$extension;
    echo -e "\xF0\x9f\x8d\xba Compression done! \nOutput: ${originalName}$extension, $outputSize";
    remove_temp_folder;
    exit;
    fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');

    if [[ "$fileOutput" -ge "$originalLsSize" ]]
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n**** The minimum image quality factor of 20 might has already been reached. **** \n************* It is not adviced to go below this quality factor. *************** \n******************************************************************************** \n";
    remove_temp_folder;
    exit;
    else
    if [[ $twenty_kb -le $outputSize ]]; then
    echo -e "\n******************************************************************************** \n*********************** Compression has been performed! ************************ \n******************************************************************************** \nThe minimum quality factor has been set to 20 to avoid the image to be corrupted \n*********** Note that the size of the new file is superior as 20Kb! ************ \n******************************************************************************** \n\n\xF0\x9f\x8d\xba Output: ${originalName}$extension, $outputSize \n";
    else
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \nOutput: ${originalName}$extension, $outputSize \n";
    fi
    remove_temp_folder;
    exit;
    fi
    }

    run_jpegoptim_quality()
    {
    if $overwrite
    then
    currentFile=$filePath;
    extension=".$imageType";
    else
    make_image_copy;
    currentFile=$tempFilePath;
    extension=$fileExtension;
    else
    currentFile=$filePath;
    extension=$imageType;
    fi

    jpegoptim -v --max=$qualityCompression "$currentFile";
    mv $currentFile ${originalName}$extension;
    echo -e "\xF0\x9f\x8d\xba Compression done!";
    remove_temp_folder;
    exit;
    fileOutput=$(ls -l "${originalName}$extension" | awk '{print $5}');

    if [[ "$fileOutput" -ge "$originalLsSize" ]]
    then
    echo -e "\n******************************************************************************** \n********************** No compression has been performed! **********************\n******************************************************************************** \nThe size of the new file is equal or greater than the size of the original file. \n You should decrease the image quality factor to decrease the size of the file. \n******************************************************************************** \n";
    remove_temp_folder;

    case $overwrite in
    (false) rm ${originalName}$extension;;
    esac

    exit;
    else
    echo -e "\n\xF0\x9f\x8d\xba Compression done! \n";
    remove_temp_folder;
    exit;
    fi
    }

    make_image_copy()
    {
    temp="/temp";
    temp="temp/";
    mkdir ${pwd}$temp;
    tempName="/_temp";
    tempName="_temp";
    tempDir=${pwd}$temp;
    cp $filePath ${tempDir}$tempName;
    tempFilePath=${tempDir}$tempName;
    @@ -200,8 +240,8 @@ run_jpegoptim_command()
    while true; do
    read -p "Do you wish to overwrite your image (Y/N)? " yn
    case $yn in
    [Yy]* ) overwrite=false; break;;
    [Nn]* ) overwrite=true; break;;
    [Yy]* ) overwrite=true; break;;
    [Nn]* ) overwrite=false; break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
  10. @caubry caubry revised this gist Dec 2, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -272,11 +272,11 @@ fi

    if [[ "$imageType" == *$pngExt* ]]
    then
    run_pngquant;
    run_pngquant;
    exit;
    elif [[ "$imageType" == *$jpgExt* ]] || [[ "$imageType" == *$jpegExt* ]]
    then
    run_jpegoptim;
    run_jpegoptim;
    exit;
    else
    echo "Compression works with PNG, JPG and JPEG only.";
  11. @caubry caubry revised this gist Dec 2, 2013. 1 changed file with 91 additions and 29 deletions.
    120 changes: 91 additions & 29 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    #!bin/bash
    pwd=`pwd`

    echo -n "Please enter the file path: ";
    read filePath;

    @@ -8,7 +10,7 @@ then
    exit;
    fi

    imageType="${filePath:(-5)}";
    imageType="${filePath##*.}";
    lengthPath="${#filePath}";
    originalName="${filePath:0:($lengthPath-4)}";

    @@ -49,10 +51,10 @@ run_pngquant()
    {
    command -v pngquant >/dev/null 2>&1 ||
    {
    echo -e >&2 "Pngquant command line tool hasn't be installed.";
    echo -e >&2 "Pngquant command line tool hasn't been installed.";

    while true; do
    read -p "Do you wish to install this program (Y/N)?" yn
    read -p "Do you wish to install this program (Y/N)? " yn
    case $yn in
    [Yy]* ) install_pngquant;;
    [Nn]* ) echo "End"; exit;;
    @@ -93,7 +95,7 @@ install_libjpeg()
    pause 'Press [Enter] key to install jpegoptim...';
    install_jpegoptim;
    else
    echo -e "Something went horribly wrong and libjpeg hasn't been installed! (╯°□°)╯︵ ┻━┻ \nDo you have an Internet access?";
    echo -e "Something went horribly wrong and libjpeg hasn't been installed! (╯°□°)╯︵ ┻━┻ \nDo you have Internet access?";
    exit;
    fi
    fi
    @@ -123,46 +125,98 @@ install_jpegoptim()
    pause 'Press [Enter] key to compress your image...';
    run_jpegoptim;
    else
    echo -e "Couldn't get jpegoptim. \nDo you have an Internet access?";
    echo -e "Couldn't get jpegoptim. \nDo you have Internet access?";
    exit;
    fi
    }

    run_jpegoptim_twenty_kb()
    {
    make_image_copy;

    twenty_kb=20000;
    copy_ext="_copy";
    copyTempFilePath=${tempFilePath}$copy_ext;
    cp $tempFilePath $copyTempFilePath;

    while [ $twenty_kb -le $outputSize -a $qualityCompression -gt 20 ]
    do
    qualityCompression=$((qualityCompression-1))
    jpegoptim -v --max=$qualityCompression "$filePath";
    outputSize=$(ls -l "$filePath" | awk '{print $5}');
    cp $copyTempFilePath $tempFilePath;
    qualityCompression=$((qualityCompression-1));
    jpegoptim -v --max=$qualityCompression "$tempFilePath";
    outputSize=$(ls -l "$tempFilePath" | awk '{print $5}');
    done

    echo -e "\xF0\x9f\x8d\xba Compression done! \nOutput: $filePath, $outputSize";
    if $overwrite
    then
    extension=$fileExtension;
    else
    extension=$imageType;
    fi

    rm $copyTempFilePath;
    mv $tempFilePath ${originalName}$extension;
    echo -e "\xF0\x9f\x8d\xba Compression done! \nOutput: ${originalName}$extension, $outputSize";
    remove_temp_folder;
    exit;
    }

    run_jpegoptim_quality()
    {
    jpegoptim -v --max=$qualityCompression "$filePath";
    if $overwrite
    then
    make_image_copy;
    currentFile=$tempFilePath;
    extension=$fileExtension;
    else
    currentFile=$filePath;
    extension=$imageType;
    fi

    jpegoptim -v --max=$qualityCompression "$currentFile";
    mv $currentFile ${originalName}$extension;
    echo -e "\xF0\x9f\x8d\xba Compression done!";
    remove_temp_folder;
    exit;
    }

    make_image_copy()
    {
    temp="/temp";
    mkdir ${pwd}$temp;
    tempName="/_temp";
    tempDir=${pwd}$temp;
    cp $filePath ${tempDir}$tempName;
    tempFilePath=${tempDir}$tempName;
    }

    remove_temp_folder()
    {
    rm -rf ${pwd}$temp;
    }

    run_jpegoptim_command()
    {
    while true; do
    read -p "Do you wish to overwrite your image (Y/N)? " yn
    case $yn in
    [Yy]* ) overwrite=false; break;;
    [Nn]* ) overwrite=true; break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done

    if $once
    then
    while true; do
    read -p "Please enter the maximum image quality factor [0-100]: " qualityCompression;
    case $qualityCompression in
    [0-100]* ) run_jpegoptim_quality;;
    * ) echo "Not a number between 0 and 100.";;
    [0-99]*) run_jpegoptim_quality;;
    * ) echo "$qualityCompression, Not a number between 0 and 100.";;
    esac
    done
    else
    qualityCompression=60;
    qualityCompression=80;
    jpegoptim -v --max=$qualityCompression "$filePath";
    outputSize=$(ls -l "$filePath" | awk '{print $5}');
    run_jpegoptim_twenty_kb;
    @@ -173,10 +227,10 @@ run_jpegoptim()
    {
    command -v jpegoptim >/dev/null 2>&1 ||
    {
    echo -e >&2 "Jpegoptim hasn't be installed.";
    echo -e >&2 "Jpegoptim hasn't been installed.";

    while true; do
    read -p "Do you wish to install this program (Y/N)?" yn
    read -p "Do you wish to install this program (Y/N)? " yn
    case $yn in
    [Yy]* ) install_libjpeg;;
    [Nn]* ) echo "End"; exit;;
    @@ -196,25 +250,33 @@ run_jpegoptim()
    done
    }

    customExt="_MIN";
    pngExt=".png";
    jpgExt=".jpg";
    jpegExt=".jpeg";
    customExt="_MINI";
    pngExt="png";
    jpgExt="jpg";
    jpegExt="jpeg";

    if [[ "$imageType" == *$pngExt* ]]
    case ${imageType:(-1)} in
    [A-Z]*) upperLetter=true;;
    [a-z]*) upperLetter=false;;
    esac

    if $upperLetter
    then
    fileExtension="${customExt}$pngExt";
    run_pngquant;
    exit;
    elif [[ "$imageType" == *$jpgExt* ]]
    fileExtension="${customExt}".$imageType"";
    lowerCaseLetters=$(echo $imageType | awk '{print tolower($0)}');
    imageType="$lowerCaseLetters";
    else
    imageType="$imageType";
    fileExtension="${customExt}".$imageType"";
    fi

    if [[ "$imageType" == *$pngExt* ]]
    then
    fileExtension="${customExt}$jpgExt";
    run_jpegoptim;
    run_pngquant;
    exit;
    elif [[ "$imageType" == *$jpegExt* ]]
    elif [[ "$imageType" == *$jpgExt* ]] || [[ "$imageType" == *$jpegExt* ]]
    then
    fileExtension="${customExt}$jpegExt";
    run_jpegoptim;
    run_jpegoptim;
    exit;
    else
    echo "Compression works with PNG, JPG and JPEG only.";
  12. @caubry caubry revised this gist Nov 5, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -204,17 +204,17 @@ jpegExt=".jpeg";
    if [[ "$imageType" == *$pngExt* ]]
    then
    fileExtension="${customExt}$pngExt";
    run_pngquant;
    run_pngquant;
    exit;
    elif [[ "$imageType" == *$jpgExt* ]]
    then
    fileExtension="${customExt}$jpgExt";
    run_jpegoptim;
    run_jpegoptim;
    exit;
    elif [[ "$imageType" == *$jpegExt* ]]
    then
    fileExtension="${customExt}$jpegExt";
    run_jpegoptim;
    run_jpegoptim;
    exit;
    else
    echo "Compression works with PNG, JPG and JPEG only.";
  13. @caubry caubry revised this gist Nov 5, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ install_pngquant()

    command -v pngquant >/dev/null 2>&1 ||
    {
    echo "Something went horribly wrong and pngquant hasn't been installed! (╯°□°)╯︵ ┻━┻)";
    echo "Something went horribly wrong and pngquant hasn't been installed! (╯°□°)╯︵ ┻━┻";
    exit;
    }

    @@ -93,7 +93,7 @@ install_libjpeg()
    pause 'Press [Enter] key to install jpegoptim...';
    install_jpegoptim;
    else
    echo -e "Something went horribly wrong and libjpeg hasn't been installed! (╯°□°)╯︵ ┻━┻) \nDo you have an Internet access?";
    echo -e "Something went horribly wrong and libjpeg hasn't been installed! (╯°□°)╯︵ ┻━┻ \nDo you have an Internet access?";
    exit;
    fi
    fi
    @@ -115,7 +115,7 @@ install_jpegoptim()

    command -v jpegoptim >/dev/null 2>&1 ||
    {
    echo "Something went horribly wrong and jpegoptim hasn't been installed! (╯°□°)╯︵ ┻━┻)";
    echo "Something went horribly wrong and jpegoptim hasn't been installed! (╯°□°)╯︵ ┻━┻";
    exit;
    }

    @@ -219,4 +219,4 @@ then
    else
    echo "Compression works with PNG, JPG and JPEG only.";
    exit;
    fi
    fi
  14. @caubry caubry revised this gist Nov 5, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions imageCompression.sh
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ install_pngquant()

    command -v pngquant >/dev/null 2>&1 ||
    {
    echo "Something went horribly wrong and pngquant hasn't been installed!";
    echo "Something went horribly wrong and pngquant hasn't been installed! (╯°□°)╯︵ ┻━┻)";
    exit;
    }

    @@ -93,7 +93,7 @@ install_libjpeg()
    pause 'Press [Enter] key to install jpegoptim...';
    install_jpegoptim;
    else
    echo -e "Something went horribly wrong and libjpeg hasn't been installed! \nDo you have an Internet access?";
    echo -e "Something went horribly wrong and libjpeg hasn't been installed! (╯°□°)╯︵ ┻━┻) \nDo you have an Internet access?";
    exit;
    fi
    fi
    @@ -115,7 +115,7 @@ install_jpegoptim()

    command -v jpegoptim >/dev/null 2>&1 ||
    {
    echo "Something went horribly wrong and jpegoptim hasn't been installed!";
    echo "Something went horribly wrong and jpegoptim hasn't been installed! (╯°□°)╯︵ ┻━┻)";
    exit;
    }

    @@ -219,4 +219,4 @@ then
    else
    echo "Compression works with PNG, JPG and JPEG only.";
    exit;
    fi
    fi
  15. @caubry caubry renamed this gist Nov 5, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  16. @caubry caubry revised this gist Nov 5, 2013. 1 changed file with 214 additions and 14 deletions.
    228 changes: 214 additions & 14 deletions pngCompression.sh
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,222 @@
    #!bin/bash
    echo -n "Please enter the file path: "
    read filePath
    echo -n "Please enter the file path: ";
    read filePath;

    if [ ! -f "$filePath" ]; then
    echo "File "$filePath" not found!"
    exit 1;
    if [ ! -f "$filePath" ]
    then
    echo "File "$filePath" not found!";
    exit;
    fi

    command -v pngquant >/dev/null 2>&1 || { echo -e >&2 "Pngquant hasn't be installed. \nPlease refer to http://pngquant.org "; exit 1; }
    imageType="${filePath:(-5)}";
    lengthPath="${#filePath}";
    originalName="${filePath:0:($lengthPath-4)}";

    fileExtension="_MINI.png"
    pause()
    {
    read -p "$*";
    }

    pngquant -f "$filePath" --ext "$fileExtension"
    install_pngquant()
    {
    cd ~/Documents/;
    git clone git://github.com/pornel/pngquant.git;

    pngExt="${filePath:(-4)}"
    lengthPath="${#filePath}"
    originalName="${filePath:0:($lengthPath-4)}"
    imageOutput="${originalName}$fileExtension"
    if [[ -d 'pngquant' ]]
    then
    cd pngquant;
    make -s;
    make -s install;
    cd ..;
    rm -rf ~/Documents/pngquant;

    outputSize=$(du -h "$imageOutput")
    echo -e "Done! \nOutput: $outputSize"
    command -v pngquant >/dev/null 2>&1 ||
    {
    echo "Something went horribly wrong and pngquant hasn't been installed!";
    exit;
    }

    echo -e "********************************** \n******* Installation done! ******* \n********************************** \n";
    pause 'Press [Enter] key to compress your image...';
    run_pngquant;
    else
    echo "Couldn't clone pngquant git repo. \nDo you have an Internet access?";
    exit;
    fi
    }

    run_pngquant()
    {
    command -v pngquant >/dev/null 2>&1 ||
    {
    echo -e >&2 "Pngquant command line tool hasn't be installed.";

    while true; do
    read -p "Do you wish to install this program (Y/N)?" yn
    case $yn in
    [Yy]* ) install_pngquant;;
    [Nn]* ) echo "End"; exit;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    exit;
    }

    pngquant -vf "$filePath" --ext "$fileExtension";
    imageOutput="${originalName}$fileExtension";
    outputSize=$(du -h "$imageOutput");
    echo -e "\xF0\x9f\x8d\xba Compression done! \nOutput: $outputSize";
    exit;
    }

    install_libjpeg()
    {
    cd /usr/local/include/;

    if [[ -f 'jpeglib.h' ]]
    then
    install_jpegoptim;
    else
    cd ~/Documents/;
    curl -O http://www.ijg.org/files/jpegsrc.v8c.tar.gz
    tar -xf jpegsrc.v8c.tar.gz
    rm jpegsrc.v8c.tar.gz

    if [[ -d 'jpeg-8c' ]]
    then
    cd jpeg-8c/;
    ./configure;
    make;
    make -s install;
    cd ..;
    rm -rf jpeg-8c/;
    pause 'Press [Enter] key to install jpegoptim...';
    install_jpegoptim;
    else
    echo -e "Something went horribly wrong and libjpeg hasn't been installed! \nDo you have an Internet access?";
    exit;
    fi
    fi
    }

    install_jpegoptim()
    {
    cd ~/Documents/;
    git clone https://github.com/tjko/jpegoptim.git;

    if [[ -d 'jpegoptim' ]]
    then
    cd jpegoptim;
    ./configure;
    make -s;
    make -s install;
    cd ..;
    rm -rf ~/Documents/jpegoptim;

    command -v jpegoptim >/dev/null 2>&1 ||
    {
    echo "Something went horribly wrong and jpegoptim hasn't been installed!";
    exit;
    }

    echo -e "********************************** \n******* Installation done! ******* \n********************************** \n";
    pause 'Press [Enter] key to compress your image...';
    run_jpegoptim;
    else
    echo -e "Couldn't get jpegoptim. \nDo you have an Internet access?";
    exit;
    fi
    }

    run_jpegoptim_twenty_kb()
    {
    twenty_kb=20000;

    while [ $twenty_kb -le $outputSize -a $qualityCompression -gt 20 ]
    do
    qualityCompression=$((qualityCompression-1))
    jpegoptim -v --max=$qualityCompression "$filePath";
    outputSize=$(ls -l "$filePath" | awk '{print $5}');
    done

    echo -e "\xF0\x9f\x8d\xba Compression done! \nOutput: $filePath, $outputSize";
    exit;
    }

    run_jpegoptim_quality()
    {
    jpegoptim -v --max=$qualityCompression "$filePath";
    echo -e "\xF0\x9f\x8d\xba Compression done!";
    exit;
    }

    run_jpegoptim_command()
    {
    if $once
    then
    while true; do
    read -p "Please enter the maximum image quality factor [0-100]: " qualityCompression;
    case $qualityCompression in
    [0-100]* ) run_jpegoptim_quality;;
    * ) echo "Not a number between 0 and 100.";;
    esac
    done
    else
    qualityCompression=60;
    jpegoptim -v --max=$qualityCompression "$filePath";
    outputSize=$(ls -l "$filePath" | awk '{print $5}');
    run_jpegoptim_twenty_kb;
    fi
    }

    run_jpegoptim()
    {
    command -v jpegoptim >/dev/null 2>&1 ||
    {
    echo -e >&2 "Jpegoptim hasn't be installed.";

    while true; do
    read -p "Do you wish to install this program (Y/N)?" yn
    case $yn in
    [Yy]* ) install_libjpeg;;
    [Nn]* ) echo "End"; exit;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    exit;
    }

    while true; do
    read -p "Do you need this image to be under 20kb (Y/N): " yn;
    case $yn in
    [Yy]* ) once=false; run_jpegoptim_command $once; break;;
    [Nn]* ) once=true; run_jpegoptim_command $once; break;;
    * ) echo "Please answer Yes or No.";;
    esac
    done
    }

    customExt="_MIN";
    pngExt=".png";
    jpgExt=".jpg";
    jpegExt=".jpeg";

    if [[ "$imageType" == *$pngExt* ]]
    then
    fileExtension="${customExt}$pngExt";
    run_pngquant;
    exit;
    elif [[ "$imageType" == *$jpgExt* ]]
    then
    fileExtension="${customExt}$jpgExt";
    run_jpegoptim;
    exit;
    elif [[ "$imageType" == *$jpegExt* ]]
    then
    fileExtension="${customExt}$jpegExt";
    run_jpegoptim;
    exit;
    else
    echo "Compression works with PNG, JPG and JPEG only.";
    exit;
    fi
  17. @caubry caubry created this gist Oct 22, 2013.
    22 changes: 22 additions & 0 deletions pngCompression.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #!bin/bash
    echo -n "Please enter the file path: "
    read filePath

    if [ ! -f "$filePath" ]; then
    echo "File "$filePath" not found!"
    exit 1;
    fi

    command -v pngquant >/dev/null 2>&1 || { echo -e >&2 "Pngquant hasn't be installed. \nPlease refer to http://pngquant.org "; exit 1; }

    fileExtension="_MINI.png"

    pngquant -f "$filePath" --ext "$fileExtension"

    pngExt="${filePath:(-4)}"
    lengthPath="${#filePath}"
    originalName="${filePath:0:($lengthPath-4)}"
    imageOutput="${originalName}$fileExtension"

    outputSize=$(du -h "$imageOutput")
    echo -e "Done! \nOutput: $outputSize"