From e398f91b3fce0d37d063ea46a00ceba6b748aa20 Mon Sep 17 00:00:00 2001 From: Elliu <elliu@hashi.re> Date: Sat, 22 Oct 2022 13:16:13 +0200 Subject: [PATCH] Fix gif generation and image dimensions Fix gif cropping for weird gifs Remove gif optimization as discord doesn't know how to display them Fix output dimensions for pictures with big alpha borders --- stpkg | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/stpkg b/stpkg index fc38692..703373b 100755 --- a/stpkg +++ b/stpkg @@ -550,14 +550,11 @@ __list() { done } -# Usage: process_sticker_file <filename> <&JSON> -process_sticker_file() { - FILE="$1" - local -n JSON_REF="$2" - - # Get sizes - local INIT_WIDTH=$(file_get_width "$FILE") - local INIT_HEIGHT=$(file_get_height "$FILE") +__get_dest_dimensions() { + local INIT_WIDTH=$1 + local INIT_HEIGHT=$2 + local MAX_WIDTH=$3 + local MAX_HEIGHT=$3 local WIDTH_RATIO="$(echo "scale=10; $INIT_WIDTH/$STPKG_MAX_WIDTH" | bc)" local HEIGHT_RATIO="$(echo "scale=10; $INIT_HEIGHT/$STPKG_MAX_HEIGHT" | bc)" @@ -578,6 +575,14 @@ process_sticker_file() { local DEST_WIDTH=$INIT_WIDTH fi + echo "${DEST_WIDTH}x${DEST_HEIGHT}" +} + +# Usage: process_sticker_file <filename> <&JSON> +process_sticker_file() { + FILE="$1" + local -n JSON_REF="$2" + # Get the names and extensions local INIT_TYPE=$(file_get_type "$FILE") local INIT_EXT=$(file_get_ext_from_type "$INIT_TYPE") @@ -606,13 +611,20 @@ process_sticker_file() { local DEST_TYPE=$STPKG_ANIMATED_TARGET_FILETYPE local DEST_EXT=$(file_get_ext_from_type "$DEST_TYPE") - convert "${FILE}" -coalesce -background none -trim -layers TrimBounds "$DEST_EXT:${TEMP}" 1>&2 \ + local backgroundColor="`convert "${FILE}[0]" -format "%[pixel:u.p{0,0}]" info:`" + convert "${FILE}" -dispose previous -background "${backgroundColor}" -trim -layers TrimBounds -coalesce "$DEST_EXT:${TEMP}" 1>&2 \ || die "$NAME failed trimming the sticker" - convert "${TEMP}" -coalesce -dispose Background -resize ${DEST_WIDTH}x${DEST_HEIGHT} "$DEST_EXT:${TEMP}" 1>&2 \ + + # Get sizes + local INIT_WIDTH=$(file_get_width "${TEMP}") + local INIT_HEIGHT=$(file_get_height "${TEMP}") + local DEST_DIMENSIONS=$(__get_dest_dimensions $INIT_WIDTH $INIT_HEIGHT $STPKG_MAX_WIDTH $STPKG_MAX_HEIGHT) + + convert "${TEMP}" -coalesce -dispose previous -resize ${DEST_DIMENSIONS} "$DEST_EXT:${TEMP}" 1>&2 \ || die "$NAME failed resizing the sticker" - #convert "${TEMP}" -coalesce -layers OptimizePlus -layers RemoveDups -layers OptimizeTransparency "$DEST_EXT:${TEMP}" 1>&2 \ - convert "${TEMP}" -coalesce -layers Optimize "$DEST_EXT:${TEMP}" 1>&2 \ - || die "$NAME failed optimizing the sticker" + + # Can't optimize the gif because discord doesn't know how to display them properly (for bridged channels) + #convert "${TEMP}" -coalesce -layers Optimize "$DEST_EXT:${TEMP}" 1>&2 \ else # @@ -621,13 +633,18 @@ process_sticker_file() { local DEST_TYPE=$STPKG_IMAGE_TARGET_FILETYPE local DEST_EXT=$(file_get_ext_from_type "$DEST_TYPE") - convert "$FILE" -bordercolor none -border 1 -background none -gravity center -trim +repage -resize ${DEST_WIDTH}x${DEST_HEIGHT} "${DEST_EXT}:${TEMP}" 1>&2 \ + # Get sizes + local INIT_WIDTH=$(file_get_width "$FILE") + local INIT_HEIGHT=$(file_get_height "$FILE") + local DEST_DIMENSIONS=$(__get_dest_dimensions $INIT_WIDTH $INIT_HEIGHT $STPKG_MAX_WIDTH $STPKG_MAX_HEIGHT) + + convert "$FILE" -bordercolor none -border 1 -background none -gravity center -trim +repage -resize ${DEST_DIMENSIONS} "${DEST_EXT}:${TEMP}" 1>&2 \ || die "$NAME failed converting the sticker" fi fi - # TODO: correctly get max width/height for animated + # Get real final dimensions for json DEST_WIDTH=$(file_get_width "$TEMP") DEST_HEIGHT=$(file_get_height "$TEMP") -- GitLab