Created
September 8, 2023 18:17
-
-
Save innermond/c7e41a1cb869128b37eecadc808bd8be to your computer and use it in GitHub Desktop.
bash:resize and upload to s3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cd "$(dirname "$0")" | |
shopt -s nocaseglob | |
rm -frv ./optimized_images/* | |
mkdir -p ./optimized_images/400w | |
AWS_PATH="<s3-uri>" | |
for f in ./unoptimized_images/*.{jpg,jpeg,png} | |
do | |
if [ -f "$f" ]; then | |
ext="${f##*.}" | |
fn=$(basename "$f") | |
fn_optimized="./optimized_images/$fn" | |
fn_optimized_webp="${fn_optimized%.*}".webp | |
fn_optimized_400w="./optimized_images/400w/$fn" | |
fn_optimized_webp_400w="${fn_optimized_400w%.*}".webp | |
ff=( "$fn_optimized" "$fn_optimized_400w" "$fn_optimized_webp" "$fn_optimized_webp_400w" ) | |
# resize into given dimensions keeping ratio or original dimensions in case the image fits entirely into given dimensions | |
for img in "${ff[@]}"; do | |
resize="700x1240>" | |
if [[ "$img" == */400w/* ]]; then | |
resize="400x" | |
fi | |
extimg="${img##*.}" | |
case "$ext" in | |
jpg|jpeg) | |
if [ "$extimg" != "webp" ]; then | |
img="${img%.*}".jpg | |
convert "$f" -resize "$resize" -sampling-factor 4:2:0 -strip -quality 80 -interlace JPEG -colorspace sRGB -sigmoidal-contrast 5,50% "$img" | |
else | |
convert "$f" -resize "$resize" -define webp:lossless=false "$img" | |
fi | |
;; | |
png) | |
if [ "$extimg" != "webp" ]; then | |
optipng "$f" -out "$fn_optimized" | |
mogrify -resize "$resize" -sigmoidal-contrast 5,50% "$img" | |
else | |
convert "$f" -resize "$resize" -define webp:lossless=false "$img" | |
fi | |
;; | |
*) | |
echo "Skypping unknown extension ["$ext"]" | |
;; | |
esac | |
aws_file="${img##*/}" | |
if [[ "$img" == */400w/* ]]; then | |
aws_file=400w/"$aws_file" | |
fi | |
aws s3 cp $img $AWS_PATH/$aws_file --acl public-read --metadata "Cache-Control=max-age=2592000" | |
done | |
fi | |
done | |
shopt -u nocaseglob |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment