-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-processor.sh
executable file
·170 lines (123 loc) · 3.9 KB
/
image-processor.sh
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env bash
path=$1
images=()
extensions=(
"jpg"
"JPG"
)
processor="magick"
height_thumb="250"
height_medium="800"
quality_thumb="70"
quality_medium="90"
folder_thumb="thumbs"
folder_medium="medium"
zip_file="download.zip"
# Load in the environment variables
source env.sh
# --- CHECK FOR ERRORS ---------------------------------------------------------
# Check if no parameters were provided
if [ $# -eq 0 ]; then
echo ""
echo "ERROR: No parameters given."
echo ""
echo "Proper syntax :"
echo " ./image-processor.sh [image-path]"
echo ""
exit 1
fi
# Ensure path exists
if [ ! -e "$path" ]; then
echo ""
echo "ERROR: [$path] does not exist."
echo ""
exit 1
fi
# Ensure that imagemagick exists
if ! command -v "$processor" >/dev/null 2>&1; then
echo ""
echo "ERROR: [$processor] is not installed or is not avilable in this path."
echo ""
exit 1
fi
# Ensure .env file exists
if [ ! -f "env.sh" ]; then
echo ""
echo "ERROR: [env.sh] does not exist. Copy it from [env.sh.sample] and edit the file."
echo ""
exit 1
fi
# Ensure the project path is defined
if [ ! "$SITE_PATH" ]; then
echo ""
echo "ERROR: [SITE_PATH] is not defined in [env.sh]."
echo ""
exit 1
fi
# Ensure AWS S3 bucket is defined
if [ ! "$IMAGE_PROCESSOR_S3_BUCKET" ]; then
echo ""
echo "ERROR: [IMAGE_PROCESSOR_S3_BUCKET] is not defined in [env.sh]."
echo ""
exit 1
fi
# ------------------------------------------------------------------------------
path_basename=$(basename "$path")
# List all JPGs in the working path
cd "$path" || { echo "Failed to change directory to [$path]"; exit 1; }
# Loop through all extensions and add any found images to the working array
for ext in "${extensions[@]}"; do
for file in *."$ext"; do
if [[ -f "$file" ]]; then
images+=("$file")
fi
done
done
# List all found images
echo "Found ${#images[@]} image files"
# Create output directories
mkdir "$path/thumbs"
mkdir "$path/medium"
# --- MAKE IMAGE VARIANTS ------------------------------------------------------
# Iterate over each image and perform transformations
for file in "${images[@]}"; do
echo "Processing $file"
webp_file=$(echo "$file" | sed -E 's/\.[jJ][pP][gG]$/.webp/')
# Create thumbnails
command=("$processor $file -auto-orient -strip -resize ${height_thumb}x${height_thumb}^ -gravity Center -extent ${height_thumb}x${height_thumb} -quality $quality_thumb $folder_thumb/$webp_file")
# echo $command
eval "$command"
# Create medium sized images
command=("$processor $file -auto-orient -strip -resize x${height_medium} -quality $quality_medium $folder_medium/$webp_file")
# echo $command
eval "$command"
done
# --- CREATE DOWNLOAD ZIP ------------------------------------------------------
echo "Creating full-size image ZIP download file"
zip "$zip_file" "${images[@]}" -q
zip_filesize="$(du -k "$zip_file" | cut -f1)"
# --- UPLOAD TO S3 -------------------------------------------------------------
command=("aws s3 cp \"$path\" s3://$IMAGE_PROCESSOR_S3_BUCKET/$path_basename --recursive --exclude \".DS_Store\"")
# echo $command
eval "$command"
# --- CREATE YAML MANIFEST -----------------------------------------------------
post_file="_posts/${path_basename}.md"
echo "Creating post: $post_file"
# Return t
cd $SITE_PATH || { echo "Failed to change directory to [$SITE_PATH]"; exit 1; }
# Clear the file if it exists (first echo line is single >)
echo "---" > $post_file
echo "title: $path_basename" >> $post_file
echo "cover: ${images[0]}" >> $post_file
echo "download_size: $zip_filesize" >> $post_file
echo "pictures:" >> $post_file
for file in "${images[@]}"; do
echo " - $file" >> $post_file
done
echo "---" >> $post_file
echo "" >> $post_file
# ------------------------------------------------------------------------------
echo ""
echo "All set!"
echo "Make any changes you want to the post, then commit the changes to the repo to deploy!"
echo ""