#!/bin/sh usage() { cat < Font size for the text. -ft Font size for top text. -fb Font size for bottom text. -s Stroke width for the text. -st Stroke width for top text. -sb Stroke width for bottom text. -fc Fill color for the text. -fct Fill color for the top text. -fcb Fill color for the bottom text. -sc Outline color for the text. -sct Outline color for top text. -scb Outline color for bottom text. Arguments: input_image Path to input image. top_text Text to be placed at the top of the image. bottom_text Text to be placed at the bottom of the image. output_image Path to output image. Defaults to "output.jpg" if not provided. EOF exit 1 } AUTORESIZE=false AUTOBREAK=false FILL_COLOR_TOP=white FILL_COLOR_BOTTOM=white STROKE_COLOR_TOP=black STROKE_COLOR_BOTTOM=black while [ $# -gt 0 ]; do case "$1" in --auto-resize) AUTORESIZE=true ;; --auto-break) AUTOBREAK=true ;; -f) shift && FONTSIZE_TOP="$1" FONTSIZE_BOTTOM="$1" ;; -ft) shift && FONTSIZE_TOP="$1" ;; -fb) shift && FONTSIZE_BOTTOM="$1" ;; -s) shift && STROKEWIDTH_TOP="$1" STROKEWIDTH_BOTTOM="$1" ;; -st) shift && STROKEWIDTH_TOP="$1" ;; -sb) shift && STROKEWIDTH_BOTTOM="$1" ;; -fc) shift && FILL_COLOR_TOP="$1" FILL_COLOR_BOTTOM="$1" ;; -fct) shift && FILL_COLOR_TOP="$1" ;; -fcb) shift && FILL_COLOR_BOTTOM="$1" ;; -sc) shift && STROKE_COLOR_TOP="$1" STROKE_COLOR_BOTTOM="$1" ;; -sct) shift && STROKE_COLOR_TOP="$1" ;; -scb) shift && STROKE_COLOR_BOTTOM="$1" ;; *) break ;; esac shift done [ "$#" -lt 1 ] && usage [ "$AUTORESIZE" = true ] && [ "$AUTOBREAK" = true ] && { TOP_TEXT=$(echo "$2" | sed "s/\(.\{$(awk -v width="$(identify -format "%w" $1)" BEGIN'{print width % 23}')\}\) /\1\n/g") BOTTOM_TEXT=$(echo "$3" | sed "s/\(.\{$(awk -v width="$(identify -format "%w" $1)" BEGIN'{print width % 23}')\}\) /\1\n/g") FONT_SIZE_TOP=${FONTSIZE_TOP:-$(awk -v width="$(identify -format "%w" "$1")" -v char="$(echo "$2" | wc -m)" 'BEGIN{printf "%g\n", (width / 7.5) - char}')} FONT_SIZE_BOTTOM=${FONTSIZE_BOTTOM:-$(awk -v width="$(identify -format "%w" "$1")" -v char="$(echo "$3" | wc -m)" 'BEGIN{printf "%g\n", (width / 7.5) - char}')} STROKE_WIDTH_TOP=${STROKEWIDTH_TOP:-$(awk -v first=$FONT_SIZE_TOP -v second="$(identify -format "%w" "$1")" 'BEGIN{printf "%.2f\n", first / sqrt(second)}')} STROKE_WIDTH_BOTTOM=${STROKEWIDTH_BOTTOM:-$(awk -v first=$FONT_SIZE_BOTTOM -v second="$(identify -format "%w" "$1")" 'BEGIN{printf "%.2f\n", first / sqrt(second)}')} convert "$1" -gravity North -pointsize "$FONT_SIZE_TOP" -font impact -fill $FILL_COLOR_TOP -stroke $STROKE_COLOR_TOP -strokewidth "$STROKE_WIDTH_TOP" -annotate +0+10 "$TOP_TEXT" \ -gravity South -pointsize "$FONT_SIZE_BOTTOM" -font impact -fill $FILL_COLOR_BOTTOM -stroke $STROKE_COLOR_BOTTOM -strokewidth "$STROKE_WIDTH_BOTTOM" -annotate +0+10 "$BOTTOM_TEXT" "${4:-output.jpg}" } && exit 0 $AUTORESIZE && { TOP_TEXT=${2:-"TOP TEXT"} BOTTOM_TEXT=${3:-"BOTTOM TEXT"} FONT_SIZE_TOP=${FONTSIZE_TOP:-$(awk -v width="$(identify -format "%w" "$1")" -v char="$(echo "$2" | wc -m)" 'BEGIN{printf "%g\n", (width / 7.5) - char}')} FONT_SIZE_BOTTOM=${FONTSIZE_BOTTOM:-$(awk -v width="$(identify -format "%w" "$1")" -v char="$(echo "$3" | wc -m)" 'BEGIN{printf "%g\n", (width / 7.5) - char}')} STROKE_WIDTH_TOP=${STROKEWIDTH_TOP:-$(awk -v first=$FONT_SIZE_TOP -v second="$(identify -format "%w" "$1")" 'BEGIN{printf "%.2f\n", first / sqrt(second)}')} STROKE_WIDTH_BOTTOM=${STROKEWIDTH_BOTTOM:-$(awk -v first=$FONT_SIZE_BOTTOM -v second="$(identify -format "%w" "$1")" 'BEGIN{printf "%.2f\n", first / sqrt(second)}')} convert "$1" -gravity North -pointsize "$FONT_SIZE_TOP" -font impact -fill $FILL_COLOR_TOP -stroke $STROKE_COLOR_TOP -strokewidth "$STROKE_WIDTH_TOP" -annotate +0+10 "$TOP_TEXT" \ -gravity South -pointsize "$FONT_SIZE_BOTTOM" -font impact -fill $FILL_COLOR_BOTTOM -stroke $STROKE_COLOR_BOTTOM -strokewidth "$STROKE_WIDTH_BOTTOM" -annotate +0+10 "$BOTTOM_TEXT" "${4:-output.jpg}" } && exit 0 $AUTOBREAK && { TOP_TEXT=$(echo "$2" | sed "s/\(.\{$(awk -v width="$(identify -format "%w" $1)" BEGIN'{print width % 23}')\}\) /\1\n/g") BOTTOM_TEXT=$(echo "$3" | sed "s/\(.\{$(awk -v width="$(identify -format "%w" $1)" BEGIN'{print width % 23}')\}\) /\1\n/g") FONT_SIZE_TOP=${FONTSIZE_TOP:-$(awk -v width="$(identify -format "%w" "$1")" 'BEGIN{printf "%g\n", width / 7.5}')} FONT_SIZE_BOTTOM=${FONTSIZE_BOTTOM:-$(awk -v width="$(identify -format "%w" "$1")" 'BEGIN{printf "%g\n", width / 7.5}')} STROKE_WIDTH_TOP=${STROKEWIDTH_TOP:-$(awk -v first=$FONT_SIZE -v second="$(identify -format "%w" "$1")" 'BEGIN{printf "%.2f\n", first / sqrt(second)}')} STROKE_WIDTH_BOTTOM=${STROKEWIDTH_BOTTOM:-$(awk -v first=$FONT_SIZE -v second="$(identify -format "%w" "$1")" 'BEGIN{printf "%.2f\n", first / sqrt(second)}')} convert "$1" -gravity North -pointsize "$FONT_SIZE_TOP" -font impact -fill $FILL_COLOR_TOP -stroke $STROKE_COLOR_TOP -strokewidth "$STROKE_WIDTH_TOP" -annotate +0+10 "$TOP_TEXT" \ -gravity South -pointsize "$FONT_SIZE_BOTTOM" -font impact -fill $FILL_COLOR_BOTTOM -stroke $STROKE_COLOR_BOTTOM -strokewidth "$STROKE_WIDTH_BOTTOM" -annotate +0+10 "$BOTTOM_TEXT" "${4:-output.jpg}" } && exit 0 TOP_TEXT=${2:-"TOP TEXT"} BOTTOM_TEXT=${3:-"BOTTOM TEXT"} FONT_SIZE=$(awk -v width="$(identify -format "%w" "$1")" 'BEGIN{printf "%g\n", width / 7.5}') STROKE_WIDTH=$(awk -v first=$FONT_SIZE -v second="$(identify -format "%w" "$1")" 'BEGIN{printf "%.2f\n", first / sqrt(second)}') convert "$1" -gravity North -pointsize "$FONT_SIZE" -font impact -fill $FILL_COLOR_TOP -stroke $STROKE_COLOR_TOP -strokewidth "$STROKE_WIDTH" -annotate +0+10 "$TOP_TEXT" \ -gravity South -pointsize "$FONT_SIZE" -font impact -fill $FILL_COLOR_BOTTOM -stroke $STROKE_COLOR_BOTTOM -strokewidth "$STROKE_WIDTH" -annotate +0+10 "$BOTTOM_TEXT" "${4:-output.jpg}"