From aa48304220cba8f6d9def72072d2a3e1ff14c84b Mon Sep 17 00:00:00 2001 From: Lucas Vivohost Date: Mon, 14 Sep 2026 10:18:13 +0200 Subject: [PATCH] feat(images): unify image resize and PNG conversion scripts feat(images): consolidate resize automation workflow - Merge automatic and manual resize scripts - Detect portrait and landscape orientation automatically - Resize portrait images to x1080 - Resize landscape images to 1920px width - Support user-defined ImageMagick resize arguments - Convert JPG/JPEG files to PNG format - Create output directory automatically - Normalize filenames by replacing spaces with underscores - Add date prefix to generated files - Improve reliability with safer dimension detection - Add script documentation and usage examples --- image editing/resizeImages.sh | 98 +++++++++++++++++++++++++++++++++++ quiethost/resize.sh | 17 ------ quiethost/resizeImages.sh | 24 --------- 3 files changed, 98 insertions(+), 41 deletions(-) create mode 100755 image editing/resizeImages.sh delete mode 100755 quiethost/resize.sh delete mode 100755 quiethost/resizeImages.sh diff --git a/image editing/resizeImages.sh b/image editing/resizeImages.sh new file mode 100755 index 0000000..4c374f0 --- /dev/null +++ b/image editing/resizeImages.sh @@ -0,0 +1,98 @@ +#!/bin/bash + +############################################################################### +# Script Name: resize-images.sh +# +# Description: +# Resizes JPG/JPEG images and converts them to PNG format. +# +# When no resize argument is supplied: +# - Portrait images are resized to fit within 1080px height (x1080) +# - Landscape images are resized to fit within 1920px width (1920) +# +# When a resize argument is supplied: +# - The supplied resize value is used for all images. +# +# Additional features: +# - Creates output directory NEW/ automatically. +# - Optionally normalizes filenames by replacing spaces with underscores. +# - Adds an optional date prefix to generated filenames. +# - Converts JPG/JPEG images to PNG. +# +# Requirements: +# - Bash +# - ImageMagick (identify, convert) +# +# Usage: +# +# Automatic portrait/landscape sizing: +# ./resize-images.sh +# +# Resize all images to width 2560: +# ./resize-images.sh 2560 +# +# Resize all images to height 1440: +# ./resize-images.sh x1440 +# +# Output: +# NEW/filename.png +# +############################################################################### + +DATE=$(date +"%d%m%y_") +DEFAULT_RESIZE="auto" + +# Use supplied resize value or automatic mode +if [[ -n "${1:-}" ]]; then + DEFAULT_RESIZE="$1" +fi + +# Create output directory +mkdir -p NEW + +for file in *.jpg *.jpeg *.JPG *.JPEG; do + + [[ -f "$file" ]] || continue + + # Replace spaces in filenames + file_new="$(echo "$file" | tr ' ' '_')" + + if [[ "$file_new" != "$file" ]]; then + echo "Rename: $file -> $file_new" + mv "$file" "$file_new" + fi + + resize_var="$DEFAULT_RESIZE" + + # Automatic resize mode + if [[ "$resize_var" == "auto" ]]; then + + dimensions=$(identify -quiet -format "%wx%h" "$file_new" 2>/dev/null) + + width=${dimensions%x*} + height=${dimensions#*x} + + if [[ -n "$width" && -n "$height" ]]; then + if (( height > width )); then + resize_var="x1080" + else + resize_var="1920" + fi + else + echo "Skipping invalid image: $file_new" + continue + fi + fi + + output="NEW/${DATE}$(basename "${file_new%.*}").png" + + echo "Transform: $file_new -> $output ($resize_var)" + + convert -quiet \ + "$file_new" \ + -resize "$resize_var" \ + "$output" + +done + +echo "Finished." \ No newline at end of file diff --git a/quiethost/resize.sh b/quiethost/resize.sh deleted file mode 100755 index 56485c6..0000000 --- a/quiethost/resize.sh +++ /dev/null @@ -1,17 +0,0 @@ -resize_var="x1080" -if [ "$1" != "" ]; then resize_var="$1"; fi -for file in *jpg ; do - if [ "$1" == "" ]; then - height="$(identify -quiet ${file} | cut -f3 -d' ' | cut -f2 -d'x')" - width="$(identify -quiet ${file} | cut -f3 -d' ' | cut -f1 -d'x')" - if [ "${height}" != "" ] && [ "${width}" != "" ]; then - if [ ${height} -gt ${width} ]; then - resize_var="x1080" - else - resize_var="1920" - fi - fi - fi - echo "Transform $file to ${resize_var}" - convert -quiet "$file" -resize ${resize_var} $(echo "NEW/${file}" | sed 's/jp[e]\?g/png/g') -done diff --git a/quiethost/resizeImages.sh b/quiethost/resizeImages.sh deleted file mode 100755 index ad242e4..0000000 --- a/quiethost/resizeImages.sh +++ /dev/null @@ -1,24 +0,0 @@ -resize_var="x1080" -DATE=$(date +"%d%m%y_") -if [ "$1" != "" ]; then resize_var="$1"; fi -for file in *jpg ; do - if [ "$1" == "" ]; then - height="$(identify -quiet ${file} | cut -f3 -d' ' | cut -f2 -d'x')" - width="$(identify -quiet ${file} | cut -f3 -d' ' | cut -f1 -d'x')" - if [ "${height}" != "" ] && [ "${width}" != "" ]; then - if [ ${height} -gt ${width} ]; then - resize_var="x1080" - else - resize_var="1920" - fi - fi - fi - file_new="$(echo ${file} | tr ' ' '_')" - if [[ "${file_new}" != "${file}" ]]; then - echo "Move $file to $file_new" - mv "$file" "$file_new" - fi - echo "Transform $file_new to ${resize_var}" - if [ ! -d NEW ]; then mkdir "NEW"; fi - convert -quiet "$file_new" -resize ${resize_var} $(echo "NEW/${DATE}${file_new}" | sed 's/jp[e]\?g/png/g') -done