From 8fa61211042cfe88f6f68041b7f7f1afbbfee3f7 2020-06-29 04:16:43 From: Branko Majic Date: 2020-06-29 04:16:43 Subject: [PATCH] Checked-in a script for resizing images up to a maximum height/width. --- diff --git a/multimedia/imgresize.sh b/multimedia/imgresize.sh new file mode 100755 index 0000000000000000000000000000000000000000..3ea48af669a3d1e0d965477a5503c3ff7ea05132 --- /dev/null +++ b/multimedia/imgresize.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# +# imgresize.sh +# +# Copyright (C) 2009, Branko Majic +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +program="imgresize.sh" +version="0.2" + +function usage() { + cat <. +EOF +} + +function version() { + cat < | +| | +| This program is free software: you can redistribute it and/or modify | +| it under the terms of the GNU General Public License as published by | +| the Free Software Foundation, either version 3 of the License, or | +| (at your option) any later version. | +| | +| This program is distributed in the hope that it will be useful, | +| but WITHOUT ANY WARRANTY; without even the implied warranty of | +| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | +| GNU General Public License for more details. | +| | +| You should have received a copy of the GNU General Public License | +| along with this program. If not, see . | ++-----------------------------------------------------------------------+ + +EOF +} + +# If no arguments were given, just show usage help. +if [[ -z $1 ]]; then + usage + exit 0 +fi + +# Parse the arguments +while getopts "vh" opt; do + case "$opt" in + v) version + exit 0;; + h) usage + exit 0;; + *) usage + exit 1;; + esac +done +i=$OPTIND +shift $(($i-1)) + +max="$1" +inputDir="$2" +outputDir="$3" + +for file in $(find "$inputDir" -iregex '.*jpg'); do + fileName="$(basename "$file")" + geometry="$(identify "$file" | sed -e "s#$file ##" | awk '{print $2}')" + width="$(echo "$geometry" | sed 's/x.*//')" + height="$(echo "$geometry" | sed 's/.*x//')" + if (( $width > $height && $width > $max )); then + convert -resize $max -quality 85 "$file" "$outputDir/$fileName" + elif (( $height > $width && $height > $max)); then + convert -resize x$max -quality 85 "$file" "$outputDir/$fileName" + else + convert "$file" -quality 85 "$outputDir/$fileName" + fi +done +