commit 94f767fd029938062065a3557f55c19c60bac336
parent 2c647f7f47a8bbfa2ea9f852c58667454304a456
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Thu, 6 Aug 2020 19:02:55 -0400
Add image resizing using 'convert' program
Diffstat:
M | compile | | | 38 | +++++++++++++++++++++++++++++++++----- |
1 file changed, 33 insertions(+), 5 deletions(-)
diff --git a/compile b/compile
@@ -37,6 +37,10 @@ if [ ! -x "$(command -v pandoc)" ]; then
echo "The program 'pandoc' is needed but was not found."
exit 1
fi
+if [ ! -x "$(command -v convert)" ]; then
+ echo "The program 'convert' from ImageMagick is needed but was not found."
+ exit 1
+fi
# Define colours
CLEAR="\033[0m"
@@ -111,7 +115,7 @@ auto_upload()
fi
}
-compile_img()
+optimize_img()
{
print_header_msg "OPTIMIZING IMG: $file"
outputFile="$imgDir/$fileName.$fileExt"
@@ -133,7 +137,18 @@ compile_img()
fi
}
-compile_css()
+resize_img()
+{
+ print_header_msg "RESIZING IMG: $file"
+ outputFile="$imgDir/$fileName.$fileExt"
+ convert "$file" -resize "$resizeFactor"% "$outputFile"
+ if [ $? != 0 ]; then
+ print_error_msg "There was an error resizing the image."
+ exit 1
+ fi
+}
+
+minify_css()
{
print_header_msg "MINIFYING CSS: $file"
outputFile="$cssDir/$fileName.min.css"
@@ -271,6 +286,7 @@ while test $# -gt 0; do
echo "-s|--strict use strict markdown compilation"
echo "-a|--auto-upload auto-upload generated files"
echo "-n|--no-auto-upload do not auto-upload the generated files"
+ echo "-r|--resize NUMBER resize input image by NUMBER percent"
exit 0
;;
-l|--loose)
@@ -289,6 +305,11 @@ while test $# -gt 0; do
autoUpload="true"
shift
;;
+ -r|--resize)
+ shift
+ resizeFactor="$1"
+ shift
+ ;;
--|*)
break
;;
@@ -312,14 +333,21 @@ for file in "$@"; do
subDir=$(echo "$fileDir" | sed "s/^pages//")
if [ "$fileExt" = "css" ]; then
- compile_css
+ minify_css
auto_upload
elif [ "$fileExt" = "md" ]; then
compile_md
auto_upload
elif [ "$fileExt" = "png" ] || [ "$fileExt" = "jpg" ] || [ "$fileExt" = "jpeg" ]; then
- compile_img
- auto_upload
+ if [ -n "$resizeFactor" ]; then
+ fileName="$fileName-thumb"
+ resize_img
+ else
+ optimize_img
+ auto_upload
+ fi
+ else
+ print_error_msg "ERROR: nothing to do."
fi
print_success_msg "FINISHED: https://$siteName/$subDir/$fileName.$fileExt"