Skip to content
Snippets Groups Projects
Commit e919f342 authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Merge branch '5-add-manage-images-scripts' into 'master'

Resolve "Add manage images scripts"

Closes #5

See merge request !4
parents 260b981f 172e3086
No related branches found
No related tags found
1 merge request!4Resolve "Add manage images scripts"
Pipeline #1187 passed
{
"images": [
"acb90f2491a73ba81e9f3d540ba66ffb",
"78edb7a85c822c76eae85da9fcd31324",
"0962959cf3392e84b9816de7fd80b656",
"78edb7a85c822c76eae85da9fcd31324",
"7c2f42e74e249ce9dad4a0802dde22c2",
"acb90f2491a73ba81e9f3d540ba66ffb",
""
]
}
*.jpg
*.png
#!/usr/bin/env bash
command -v convert >/dev/null 2>&1 || { echo >&2 "I require convert (imagemagick) but it's not installed. Aborting."; exit 1; }
command -v optipng >/dev/null 2>&1 || { echo >&2 "I require optipng 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_RAW_FOLDER="${IMAGES_CACHE_FOLDER}/01_raw_images"
IMAGES_OPTIMIZED_FOLDER="${IMAGES_CACHE_FOLDER}/02_optimized_images"
RESIZE_OPTION="640x640"
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 {} \;
echo "Cleaning existing optimized images..."
find "${IMAGES_OPTIMIZED_FOLDER}" -type f -name "*.png" -exec rm {} \;
IMAGES="$(find "${IMAGES_RAW_FOLDER}" -type f -name "*.??g" | sort)"
while read -r INPUT_FILE; do
if [[ -n "${INPUT_FILE}" ]]; then
HASH="$(echo "${INPUT_FILE%.*}" | md5sum | cut -c1-32)"
OUTPUT_FILE="${IMAGES_OPTIMIZED_FOLDER}/${HASH}.png"
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."
#!/usr/bin/env bash
command -v jq >/dev/null 2>&1 || { echo >&2 "I require jq (json parser) but it's not installed. Aborting."; exit 1; }
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
BASE_DIR="$(dirname "${CURRENT_DIR}")"
ASSETS_BASE_FOLDER="${BASE_DIR}/assets"
OUTPUT_ASSETS_FILE="${ASSETS_BASE_FOLDER}/files/images.json"
IMAGES_ASSETS_FOLDER="${ASSETS_BASE_FOLDER}/images"
touch "${OUTPUT_ASSETS_FILE}"
IMAGES_CACHE_FOLDER="${CURRENT_DIR}/cache"
IMAGES_OPTIMIZED_FOLDER="${IMAGES_CACHE_FOLDER}/02_optimized_images"
echo "Cleaning empty/temp files..."
find "${IMAGES_OPTIMIZED_FOLDER}" -type f -name "*.png" -empty -exec rm {} \;
find "${IMAGES_OPTIMIZED_FOLDER}" -type f -name "*.tmp*" -exec rm {} \;
echo "Move new optimized images..."
find "${IMAGES_OPTIMIZED_FOLDER}" -type f -name "*.png" -exec mv -v {} "${IMAGES_ASSETS_FOLDER}" \;
echo "Building assets json file..."
FILES="$(find "${IMAGES_ASSETS_FOLDER}" -type f -name "*.png" | sed "s|^${IMAGES_OPTIMIZED_FOLDER}/||g" | sort)"
OUTPUT_ASSETS_FILE_TMP="${OUTPUT_ASSETS_FILE}.tmp"
echo "{" > "${OUTPUT_ASSETS_FILE_TMP}"
echo " \"images\": [" >> "${OUTPUT_ASSETS_FILE_TMP}"
while read -r FILE; do
FILE_CODE="$(basename "${FILE%.*}")"
if [[ -n "${FILE}" ]]; then
echo "- ${FILE_CODE}"
echo " \"${FILE_CODE}\"," >> "${OUTPUT_ASSETS_FILE_TMP}"
fi
done < <(echo "${FILES}")
echo " \"\"" >> "${OUTPUT_ASSETS_FILE_TMP}"
echo " ]" >> "${OUTPUT_ASSETS_FILE_TMP}"
echo "}" >> "${OUTPUT_ASSETS_FILE_TMP}"
# Format json
cat "${OUTPUT_ASSETS_FILE_TMP}" | jq > "${OUTPUT_ASSETS_FILE}"
rm "${OUTPUT_ASSETS_FILE_TMP}"
echo "done."
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment