From 1bf37d0a5ca02ed1524a371477c546b9636263e6 Mon Sep 17 00:00:00 2001
From: Tigriz <tigriz@nigge.rs>
Date: Thu, 17 Sep 2020 13:59:23 +0200
Subject: [PATCH] Resizing to 256x256>, json is 128x128 forced

---
 matrixpack.sh | 126 ++++++++++++++++++++++++++++----------------------
 1 file changed, 71 insertions(+), 55 deletions(-)

diff --git a/matrixpack.sh b/matrixpack.sh
index df96f95..443578b 100755
--- a/matrixpack.sh
+++ b/matrixpack.sh
@@ -28,68 +28,84 @@ for i in "$@"
 do
   if [ -d $i ]; then
 
-# Changing directory to work directly in it
-cd "$i"
+    # Changing directory to work directly in it
+    cd "$i"
 
-# Setting variables
-dir=$(pwd)
-token=$1
-packname=${dir##*/}
-slug=$(slugify "$packname")
-homeserver=$2
+    # Setting variables
+    dir=$(pwd)
+    token=$1
+    packname=${dir##*/}
+    slug=$(slugify "$packname")
+    homeserver=$2
 
-# Printing informations
-echo -e "Creating sticker pack \033[94;1m$packname\033[0m\nhomeserver=\033[94m$homeserver\033[0m\noutput=\033[94m$slug\033[0m"
+    # Printing informations
+    echo -e "Creating sticker pack \033[94;1m$packname\033[0m\nhomeserver=\033[94m$homeserver\033[0m\noutput=\033[94m$slug\033[0m"
 
-# Making result folder
-if ! [ -d $slug ]; then mkdir $slug; fi
-if ! [ -d "$slug/tmp" ]; then mkdir "$slug/tmp"; fi
-echo -n "{\"title\":\"$packname\",\"id\":\"$slug\",\"stickers\":[" > "$slug/$slug.json"
+    # Making result folder
+    if ! [ -d $slug ]; then mkdir $slug; fi
+    if ! [ -d "$slug/tmp" ]; then mkdir "$slug/tmp"; fi
+    echo -n "{\"title\":\"$packname\",\"id\":\"$slug\",\"stickers\":[" > "$slug/$slug.json"
 
-first=""
-for f in *
-do
-  # Ignore folders
-  if [ -f "$f" ]; then
-    echo -n "$f : "
-    type="png"
-    opts="-type TrueColor PNG32:"
+    first=""
+    for f in *
+    do
+      # Ignore folders
+      if [ -f "$f" ]; then
 
-    # Gif
-    if [[ "$f" == *.gif ]]; then
-      type="gif"
-      opts=""
-    fi
-    # Trim, resize and remove indexed palette from image
-    echo -n "trimming and resizing"
-    convert "$f" -bordercolor none -border 1 "$slug/tmp/$f.$type"
-    echo -n "."
-    convert "$slug/tmp/$f.$type" -trim +repage "$slug/tmp/$f.$type"
-    echo -n "."
-    convert -background none -gravity center "$slug/tmp/$f.$type" -resize "128x128>" -extent 128x128 $opts"$slug/tmp/$f.$type"
-    echo -ne ". \033[92mdone\033[0m! "
-    
-    # First item in array
-    echo -n "$first" >> "$slug/$slug.json"
-    
-    # Uploading image
-    echo -n "uploading."
-    mxc=$(curl -s -X POST -H "Content-Type: image/$type" --data-binary "@$slug/tmp/$f.$type" "https://$homeserver/_matrix/media/r0/upload?access_token=$token" | python3 -c "import sys, json; print(json.load(sys.stdin)['content_uri'])")
-    echo -n "."
+        # Resizing large images
+        width=$(identify -format "%w" "$f")> /dev/null
+        height=$(identify -format "%h" "$f")> /dev/null
+        if [ $width -gt 256 ]; then
+          width="256";
+        fi
+        if [ $height -gt 256 ]; then
+          height="256";
+        fi
 
-    # Appending to json
-    echo -n "{\"body\":\"$f\",\"info\":{\"mimetype\":\"image/$type\",\"h\":128,\"w\":128,\"thumbnail_url\":\"$mxc\"},\"msgtype\":\"m.sticker\",\"url\":\"$mxc\",\"id\":\"$packname-$f\"}" >> "$slug/$slug.json"
-    first=","
-    echo -e ". \033[92msuccess\033[0m!"
-  fi
-done
+        echo -n "$f : "
+        type="png"
+        opts="-type TrueColor PNG32:"
+
+        # Gif
+        if [[ "$f" == *.gif ]]; then
+          type="gif"
+          opts=""
+        fi
+        # Trim, resize and remove indexed palette from image
+        echo -n "trimming and resizing"
+        convert "$f" -bordercolor none -border 1 "$slug/tmp/$f.$type"
+        echo -n "."
+        convert "$slug/tmp/$f.$type" -trim +repage "$slug/tmp/$f.$type"
+        echo -n "."
+        convert -background none -gravity center "$slug/tmp/$f.$type" -resize "${width}x$height" $opts"$slug/tmp/$f.$type"
+        echo -ne ". \033[92mdone\033[0m! "
+        
+        # First item in array
+        echo -n "$first" >> "$slug/$slug.json"
+        
+        # Uploading image
+        echo -n "uploading."
+        mxc=$(curl -s -X POST -H "Content-Type: image/$type" --data-binary "@$slug/tmp/$f.$type" "https://$homeserver/_matrix/media/r0/upload?access_token=$token" | python3 -c "import sys, json; print(json.load(sys.stdin)['content_uri'])")
+        echo -n "."
 
-montage "$slug/tmp/*"[0] -background none "$slug/preview.png"
-rm -r "$slug/tmp"
-echo -ne "# $packname  \n![Preview of $packname](preview.png)" > "$slug/README.md"
-echo -n "]}" >> "$slug/$slug.json"
-cd - > /dev/null
-echo -e "\033[92;1mPack successfully created!\n\033[0mCheck \033[94;1m$dir/$slug \033[0mfor output"
+        # Calculating 128x128> format
+        convert "$slug/tmp/$f.$type" -resize "128x128" "$slug/tmp/size"
+        width=$(identify -format "%w" "$slug/tmp/size")> /dev/null
+        height=$(identify -format "%h" "$slug/tmp/size")> /dev/null
+        # Appending to json
+        echo -n "{\"body\":\"$f\",\"info\":{\"mimetype\":\"image/$type\",\"h\":$height,\"w\":$width,\"thumbnail_url\":\"$mxc\"},\"msgtype\":\"m.sticker\",\"url\":\"$mxc\",\"id\":\"$packname-$f\"}" >> "$slug/$slug.json"
+        first=","
+        echo -e ". \033[92msuccess\033[0m!"
+      fi
+    done
+    
+    rm "$slug/tmp/size"
+    montage "$slug/tmp/*"[0] -background none "$slug/preview.png"
+    # rm -r "$slug/tmp"
+    echo -ne "# $packname  \n![Preview of $packname](preview.png)" > "$slug/README.md"
+    echo -n "]}" >> "$slug/$slug.json"
+    cd - > /dev/null
+    echo -e "\033[92;1mPack successfully created!\n\033[0mCheck \033[94;1m$dir/$slug \033[0mfor output"
 
   fi
 done
-- 
GitLab