commit 9872e5427e83c7408b2d393b82808e1af65fb60c
parent be74625004225689f2e608d35635fbc20a7fafa9
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Wed, 2 Jan 2019 10:33:16 +0100
Check if input file is an image before performing imagemagick operations
Diffstat:
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/links/bin/upload b/links/bin/upload
@@ -29,7 +29,12 @@ function show_version {
echo "https://gitlab.com/admesg/dotfiles"
}
+function check_if_image {
+ identify "$1" &>/dev/null || die "Error: $1 is not an image"
+}
+
function add_sharpening {
+ check_if_image "$2"
convert "$1" \
-unsharp 2x0.5+0.7+0 \
-quality 95 \
@@ -37,6 +42,7 @@ function add_sharpening {
}
function add_border {
+ check_if_image "$2"
convert "$1" \
-bordercolor White -border 10%x10% \
-unsharp 2x0.5+0.7+0 \
@@ -45,6 +51,7 @@ function add_border {
}
function add_resize {
+ check_if_image "$2"
convert "$1" \
-resize 800x800\> \
-quality 95 \
@@ -109,15 +116,16 @@ while :; do
shift
done
-[ $# -lt 1 ] && (die 'Error: No input FILE[S] specified')
+[ $# -lt 1 ] && die 'Error: No input FILE[S] specified'
# loop over FILE[S]
for f in "$@"; do
tempfile="$(mktemp)"
+ [ ! -f "$f" ] && die "Error: $f is not a file"
cp "$f" "$tempfile"
- [ "$border" = 1 ] && add_border "$tempfile"
- [ "$resize" = 1 ] && add_resize "$tempfile"
- [ "$sharpen" = 1 ] && add_sharpening "$tempfile"
+ [ "$border" = 1 ] && add_border "$tempfile" "$f"
+ [ "$resize" = 1 ] && add_resize "$tempfile" "$f"
+ [ "$sharpen" = 1 ] && add_sharpening "$tempfile" "$f"
upload_file "$tempfile" "$(basename "$f")"
done