Skip to content

Optimizing uploaded images

Users can upload images for channels. Large images are not automatically scaled by Notifly and can take up unnecessary disk space and network bandwidth. The script below will resize all images so that their maximum dimension, i.e. width and/or height, is 512 pixels. Smaller images will not be resized.

PNG images will be optimized for file size without loss of quality.

Once a file has been resized/optimized, it will not be resized/optimized again later.

Install

Create a script, for example /opt/notifly/optimize-images.sh containing

#!/usr/bin/env bash
set -e
DATA=/home/jm/src/Notifly/data
for FILE in "$DATA"/images/*; do
if [ "$FILE" -nt "$DATA"/images-optimized ]; then
EXT=$(echo "${FILE##*.}"|tr '[:upper:]' '[:lower:]')
if [ "$EXT" = png -o "$EXT" = jpg -o "$EXT" = jpeg -o "$EXT" = gif ]; then
convert "$FILE" -resize "512>" "$FILE"
fi
if [ "$EXT" = png ]; then
optipng "$FILE"
fi
fi
done
touch "$DATA"/images-optimized

Note that if $DATA/images-optimized is missing, all files will be selected for resizing and optimization.

Run the following commands on the script

Окно терминала
sudo chmod go-rw /opt/notifly/optimize-images.sh
sudo chmod u+x /opt/notifly/optimize-images.sh

You can test the script by running it manually. Having a backup of the data directory beforehand is always recommended.

Add the following line to a file, for example /etc/cron.d/notifly

12 12 * * * root /opt/notifly/optimize-images.sh