Skip to content
Snippets Groups Projects
Select Git revision
  • 7b72ada6355023076dcf282f45fb3de5cf00023e
  • master default protected
  • 77-improve-app-metadata
  • 68-add-words
  • 62-fix-get-image-when-word-with-accent
  • 44-implement-game-write-word-from-letters
  • 43-add-script-to-get-images-from-assets
  • 32-add-accents-and-diacritics-in-french-words-2
  • Release_0.10.1_81 protected
  • Release_0.10.0_80 protected
  • Release_0.9.2_79 protected
  • Release_0.9.1_78 protected
  • Release_0.9.0_77 protected
  • Release_0.8.0_76 protected
  • Release_0.7.0_75 protected
  • Release_0.6.0_74 protected
  • Release_0.5.2_73 protected
  • Release_0.5.1_72 protected
  • Release_0.5.0_71 protected
  • Release_0.4.1_70 protected
  • Release_0.4.0_69 protected
  • Release_0.3.1_68 protected
  • Release_0.3.0_67 protected
  • Release_0.2.1_66 protected
  • Release_0.2.0_65 protected
  • Release_0.1.40_64 protected
  • Release_0.1.39_63 protected
  • Release_0.1.38_62 protected
28 results

03_optimize_images.sh

Blame
  • 03_optimize_images.sh 1.74 KiB
    #!/usr/bin/env bash
    
    command -v optipng >/dev/null 2>&1 || { echo >&2 "I require optipng but it's not installed. Aborting."; exit 1; }
    command -v convert >/dev/null 2>&1 || { echo >&2 "I require convert (imagemagick) but it's not installed. Aborting."; exit 1; }
    
    CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
    
    IMAGES_CACHE_FOLDER="${CURRENT_DIR}/cache"
    
    IMAGES_SELECTED_FOLDER="${CURRENT_DIR}/cache/download"
    IMAGES_OPTIMIZED_FOLDER="${CURRENT_DIR}/cache/optimized"
    
    RESIZE_OPTION="128x128"
    CROP_PARAMETERS="-auto-orient -trim +repage -gravity center -background white -extent ${RESIZE_OPTION}^"
    CONVERT_OPTIONS="-alpha off +dither -colors 256 -depth 4"
    
    OPTIPNG_OPTIONS="-preserve -quiet -o7"
    
    echo "Cleaning empty/temp files..."
    find "${IMAGES_CACHE_FOLDER}" -type f -name "*.png" -empty -exec rm {} \;
    find "${IMAGES_CACHE_FOLDER}" -type f -name "*.tmp*" -exec rm {} \;
    find "${IMAGES_CACHE_FOLDER}" -type d -empty -exec rm -rf {} \;
    
    echo "Cleaning existing optimized images..."
    find "${IMAGES_OPTIMIZED_FOLDER}" -type f -name "*.png" -exec rm {} \;
    
    IMAGES="$(find "${IMAGES_SELECTED_FOLDER}" -type f -name "*.png" | sort)"
    
    while read -r INPUT_FILE; do
      if [[ -n "${INPUT_FILE}" ]]; then
        OUTPUT_FILE="$(echo "${INPUT_FILE}" | sed "s|^${IMAGES_SELECTED_FOLDER}/|${IMAGES_OPTIMIZED_FOLDER}/|g")"
        echo "  OUTPUT_FILE: ${OUTPUT_FILE}"
    
        if [[ -f "${OUTPUT_FILE}" ]]; then
          echo "   - Already optimized"
        else
          mkdir -p "$(dirname ${OUTPUT_FILE})"
    
          echo "   + Converting..."
          convert "${INPUT_FILE}" -resize "${RESIZE_OPTION}^" ${CROP_PARAMETERS} ${CONVERT_OPTIONS} "${OUTPUT_FILE}"
          echo "   + Optimizing..."
          optipng ${OPTIPNG_OPTIONS} "${OUTPUT_FILE}"
        fi
      fi
    done < <(echo "${IMAGES}")
    
    echo "done."