feat(bin): update scripts to function format

This commit is contained in:
2024-07-23 03:45:22 +03:00
parent 4e4692321b
commit 26f6024292
23 changed files with 1038 additions and 414 deletions

View File

@@ -1,39 +1,69 @@
#!/usr/bin/env bash
# Generate thumbnails using magick
# Generate thumbnails using ImageMagick (magick)
# https://imagemagick.org/script/download.php
#
# Defaults to current directory creating thumbs with 1000x1000
# images with 200px white borders around the original image.
# Defaults to current directory creating thumbnails with 1000x1000
# dimensions and 200px white borders around the original image.
#
# Defaults can be overridden with ENV variables like this:
# $ THMB_BACKGROUND=black x-thumbgen ~/images/
#
# Created by: Ismo Vuorinen <https://github.com/ivuorinen> 2015
: "${THMB_SOURCE:=$1}"
set -euo pipefail
# Default values
: "${THMB_SOURCE:=${1:-}}"
: "${THMB_BACKGROUND:=white}"
: "${THMB_RESIZE:=800x800}"
: "${THMB_EXTENT:=1000x1000}"
[ $# -eq 0 ] && {
# Print usage information
usage()
{
echo "Usage: $0 /full/path/to/image/folder"
exit 1
}
if [ "$THMB_SOURCE" == "" ] || [ ! -d "$THMB_SOURCE" ]; then
THMB_SOURCE=$(pwd)
fi
# Check if ImageMagick is installed
check_magick_installed()
{
if ! command -v magick &> /dev/null; then
echo "magick not found in PATH, https://imagemagick.org/script/download.php"
exit 1
fi
}
if command -v magick &> /dev/null; then
# Generate thumbnails
generate_thumbnails()
{
local source=$1
magick \
"$THMB_SOURCE/*" \
"${source}/*" \
-resize "$THMB_RESIZE" \
-background "$THMB_BACKGROUND" \
-gravity center \
-extent "$THMB_EXTENT" \
-set filename:fname '%t_thumb.%e' +adjoin '%[filename:fname]'
}
else
echo "magick not found in PATH, https://imagemagick.org/script/download.php"
fi
# Main function
main()
{
# Validate input
if [ -z "$THMB_SOURCE" ]; then
usage
fi
# Check if the source directory is valid
if [ ! -d "$THMB_SOURCE" ]; then
echo "Invalid directory: $THMB_SOURCE"
exit 1
fi
check_magick_installed
generate_thumbnails "$THMB_SOURCE"
}
main "$@"