#!/usr/bin/bash
# Purpose: batch image resizer
# Source: https://guides.wp-bullet.com
# Author: Mike

# absolute path to image folder

declare -A dimensions
dimensions["2400"]="1600"
dimensions["1500"]="1000"
dimensions["150"]="150"
dimensions["100"]="100"
folders="../bilder"

for WIDTH in "${!dimensions[@]}"; do
    
    # max height
    HEIGHT=${dimensions[$WIDTH]}
    
    for FOLDER in $folders; do
        cd $FOLDER
        mkdir "${WIDTH}px"
        
        for file in $FOLDER/*.jpg; do
            #echo $file;
            filename=$(basename $file)
            targetname="${WIDTH}px/${filename}"
            
            echo "erstelle $webpName"
            convert $file -quality 90 -resize "${WIDTH}x${HEIGHT}" "$targetname";
            
            webpName="${WIDTH}px/${filename}.webp"
            echo "erstelle $webpName"
            convert $targetname -quality 90 -define webp "$webpName"
        done
        
    done
    
done
