Skip to content

Commit 959abff

Browse files
author
kx1t
committed
ensure original modtime is kept when compressing images
1 parent aa28dd2 commit 959abff

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

rootfs/scripts/post2bsky.sh

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,24 @@ for image in "${IMAGES[@]}"; do
132132
# figure out what type the image is: jpeg, png, gif, and reduce size if necessary/possible.
133133
mimetype_local="$(file --mime-type -b "$image")"
134134
imgsize_org="$(stat -c%s "$image")"
135+
modtime_org="$(stat -c "%y" "$image")"
136+
135137
if (( imgsize_org >= 950000 )); then
136-
if [[ "$mimetype_local" == "image/jpeg" ]]; then
137-
jpegoptim -q -S950 -s "$image" # if it's JPG and > 1 MB, we can optimize for it
138-
# try again if still too big
139-
if (( $(stat -c%s "$image") >= 950000 )); then
140-
jpegoptim -q -S850 -s "$image"
141-
fi
142-
elif [[ "$mimetype_local" == "image/png" ]]; then
143-
pngquant -f -o "${image}.tmp" 64 "$image" # if it's PNG and > 1 MB, we can optimize for it
144-
mv -f "${image}.tmp" "$image"
145-
else
146-
"${s6wrap[@]}" echo "Omitting image $image as it is too big ($imgsize)"
147-
continue # skip if it's not JPG or PNG
148-
fi
149-
"${s6wrap[@]}" echo "Image size of $image reduced from $imgsize_org to $(stat -c%s "$image")"
138+
if [[ "$mimetype_local" == "image/jpeg" ]]; then
139+
jpegoptim -q -S950 -s "$image" # if it's JPG and > 1 MB, we can optimize for it
140+
# try again if still too big
141+
if (( $(stat -c%s "$image") >= 950000 )); then
142+
jpegoptim -q -S850 -s "$image"
143+
fi
144+
elif [[ "$mimetype_local" == "image/png" ]]; then
145+
pngquant -f -o "${image}.tmp" 64 "$image" # if it's PNG and > 1 MB, we can optimize for it
146+
mv -f "${image}.tmp" "$image"
147+
else
148+
"${s6wrap[@]}" echo "Omitting image $image as it is too big ($imgsize)"
149+
continue # skip if it's not JPG or PNG
150+
fi
151+
touch -d "$modtime_org" "$image" # restore original modification date of the image (for cache management purposes)
152+
"${s6wrap[@]}" echo "Image size of $image reduced from $imgsize_org to $(stat -c%s "$image")"
150153
fi
151154
if (( $(stat -c%s "$image") >= 950000 )); then
152155
"${s6wrap[@]}" echo "Omitting image $image as the size reduction was insufficient: before: $imgsize_org; now: $(stat -c%s "$image")"

0 commit comments

Comments
 (0)