-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvydGenerateLinkQR
72 lines (61 loc) · 2.21 KB
/
vydGenerateLinkQR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# Define colors for logging
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to display logs with colors
function log {
COLOR=$1
MESSAGE=$2
echo -e "${!COLOR}${MESSAGE}${NC}"
}
# Function to display usage information
function usage {
echo -e "${YELLOW}Usage: $0 -u URL -o OUTPUT_FILE -w WIDTH_IN_PIXELS [optional parameters]${NC}"
echo ""
echo "Required Options:"
echo -e " ${GREEN}-u URL${NC} URL to encode into the QR code"
echo -e " ${GREEN}-o OUTPUT_FILE${NC} Output file name (e.g., url-qr.png)"
echo ""
echo "Optional Options:"
echo -e " ${GREEN}-w WIDTH_IN_PIXELS${NC} Width of the QR code in pixels (defaults to 500)"
echo -e " ${GREEN}-c FOREGROUND_COLOR (RGBA)${NC} Color of the dots in the QR code (default: 000000FF)"
echo -e " ${GREEN}-b BACKGROUND_COLOR (RGBA)${NC} Background color of the QR code (default: FFFFFFFF)"
echo ""
exit
}
# Default values
FOREGROUND_COLOR="000000FF"
BACKGROUND_COLOR="FFFFFFFF"
WIDTH_IN_PIXELS=500
FORMAT="png"
# Parse command-line arguments
while getopts "u:o:w:c:b:h" flag; do
case "${flag}" in
u) URL=${OPTARG};;
o) OUTPUT_FILE=${OPTARG};;
w) WIDTH_IN_PIXELS=${OPTARG};;
c) FOREGROUND_COLOR=${OPTARG};;
b) BACKGROUND_COLOR=${OPTARG};;
h) usage;;
?) usage;;
esac
done
# Check if required parameters are provided
if [[ -z "$URL" || -z "$OUTPUT_FILE" ]]; then
log "RED" "URL and output file name are required."
usage
fi
# Calculate approximate size for initial QR code generation
# The factor 4 is used as a heuristic to maintain image quality
INITIAL_SIZE=$(($WIDTH_IN_PIXELS / 4))
# Generate QR Code with the initial size
TMP_FILE=$(mktemp ./qr-XXXXXX.png)
qrencode -l M -t PNG -o "$TMP_FILE" -s $INITIAL_SIZE --foreground=$FOREGROUND_COLOR --background=$BACKGROUND_COLOR "$URL"
# Resize the image using ImageMagick with quality preservation
convert "$TMP_FILE" -resize "${WIDTH_IN_PIXELS}x" -quality 100 -sharpen 0x1.0 "$OUTPUT_FILE"
# Clean up temporary file
rm "$TMP_FILE"
# Display success message
log "GREEN" "QR code generated and resized successfully in $OUTPUT_FILE"