gifcap/gifcap.sh
2024-08-08 16:42:14 -04:00

104 lines
2.4 KiB
Bash
Executable File

#!/bin/sh
# gifcap: Make an iFunny gif caption
# Copyright (C) 2024 Sasha Koshka (pseudonymous)
#
# 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 <https://www.gnu.org/licenses/>.
name="$0"
font="Futura PT Cond Extra Bold.otf"
input=""
output=""
caption=""
usage () {
echo "Usage: $name [OPTION]... CAPTION..."
echo "Make an iFunny gif caption."
echo ""
echo " -f, --font specify a font to use (default: ifunny.otf)"
echo " -i, --input the input file (required)"
echo " -o, --output the output file (default: input)"
echo " --help display this help and exit"
}
while test $# -gt 0; do
case $1 in
-f|--font)
font="$2"
shift; shift
;;
-i|--input)
input="$2"
shift; shift
;;
-o|--output)
output="$2"
shift; shift
;;
--help)
usage
exit 0
;;
-*|--*)
usage
exit 2
;;
*)
if test -z "$caption";
then caption="$1"
else caption="$caption $1"
fi
shift
;;
esac
done
if test -z "$input"; then
usage
exit 2
fi
if test -z "$output"; then
output="captioned-`basename "$input"`"
fi
if ! test -f "$input"; then
echo "$name: $input not found"
exit 1
fi
captionWidth="512x"
captionTemp=`mktemp --suffix=".png"`
imageWidth=`convert "$input" -format "%[w]x" info:`
echo "$name: generating caption..."
convert \
-background white -fill "#010000" -font "$font" \
-pointsize 55 -size "$captionWidth" -gravity Center \
caption:"$caption" \
-bordercolor white -border 20x20 \
"$captionTemp"
echo "$name: compositing over $input..."
# https://www.imagemagick.org/discourse-server/viewtopic.php?t=33797
convert \
"$captionTemp" -resize "$imageWidth" \
-background white \( "$input" -coalesce \) \
-set page "%[fx:u.w]x%[fx:u.h+v.h]+%[fx:t?(u.w-v.w)/2:0]+%[fx:t?u.h:0]" -coalesce \
null: -insert 1 -layers composite -layers optimize \
"$output"
rm "$captionTemp"
echo "$name: done!"