Skip to content

Instantly share code, notes, and snippets.

@leosuncin
Last active August 29, 2015 14:16
Show Gist options
  • Save leosuncin/9f8245448172cb8ee40f to your computer and use it in GitHub Desktop.
Save leosuncin/9f8245448172cb8ee40f to your computer and use it in GitHub Desktop.
Resize automatically images when was uploaded to server

Resize automatically images when was uploaded to server

For watch for changes in a folder, I use incron, what is a equivalent to cronjob
See man incrontab

Prerequisites

You must first install incron and GraphicsMagick
sudo apt-get install incron graphicsmagick
Add your username in /etc/incron.allow before to use incrontab, then execute incrontab -e and add the next lines (<path to watch> <file event> <script>)

/var/www/uploads/covers IN_CLOSE_WRITE,IN_DELETE /usr/local/bin/inotify.sh $@ $# $% /var/www/uploads/profiles IN_CLOSE_WRITE,IN_DELETE /usr/local/bin/inotify.sh $@ $# $%

#!/bin/bash
usage() {
tput bel
tput setaf 2
echo -e "Configure:" >&2
tput setaf 4
which incrontab >/dev/null 2>&1 || echo "`if [[ $USER != "root" ]]; then echo 'sudo'; fi` apt-get install incron"
grep $USER /etc/incron.allow >/dev/null 2>&1 || echo "echo -e \$USER | `if [[ $USER != "root" ]]; then echo 'sudo'; fi` tee -a /etc/incron.allow"
tput setaf 2
echo "# Add this lines" >&2
tput setaf 7
echo "/var/www/uploads/covers IN_CLOSE_WRITE,IN_DELETE `pwd`/`basename $0` -d \$@ -f \$# -e \$%" >&2
echo "/var/www/uploads/profiles IN_CLOSE_WRITE,IN_DELETE `pwd`/`basename $0` -d \$@ -f \$# -e \$%" >&2
tput setaf 4
echo "incrontab -e" >&2
tput sgr0
}
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
while getopts d:f:e:h OPT; do # "{d,f,e}:" waits for an argument "h" doesn't
case $OPT in
d )
UPLOAD=`basename $OPTARG`
FULLPATH=$OPTARG
;;
f )
FILENAME=`basename $OPTARG`
;;
e )
EVT=$OPTARG
;;
h )
usage
exit 0
;;
\?) # getopts issues an error message
usage
exit 1
;;
esac
done
FULLPATH=$FULLPATH/$FILENAME
BASENAME=${0##*/}
BASENAME=${BASENAME%%.*}
MAX_NPROC=4
WORKDIR=/var/www/thumbnails
THUMBNAILS_DIR="$WORKDIR/$UPLOAD/small"
LOCK_DIR=$WORKDIR/$BASENAME.d
mkdir -p $THUMBNAILS_DIR $LOCK_DIR
message() {
logger --id --tag $BASENAME $1
}
thumbnail() {
gm convert $1 -resize $2^ -gravity center -extent $2 +profile "*" $3
message "[$EVT] resize $1 to $2 => $3"
}
while [[ $EVT = "IN_CLOSE_WRITE" && $(ls $LOCK_DIR | wc -l) -ge $MAX_NPROC ]]; do
wait
done
touch $LOCK_DIR/$$
trap 'rm -f "$LOCK_DIR/$$"; exit $?' INT TERM EXIT KILL
if [[ -f $FULLPATH && $EVT = "IN_CLOSE_WRITE" ]]; then
case $UPLOAD in
covers )
thumbnail $FULLPATH "995x501" /tmp/$FILENAME
thumbnail /tmp/$FILENAME "274x144" $THUMBNAILS_DIR/$FILENAME
mv /tmp/$FILENAME $FULLPATH
;;
profiles )
thumbnail $FULLPATH "128x128" $THUMBNAILS_DIR/$FILENAME
mv /tmp/$FILENAME $FULLPATH
;;
esac
elif [ $EVT = "IN_DELETE" ]; then
rm -f $THUMBNAILS_DIR/$FILENAME
message "[$EVT] delete $THUMBNAILS_DIR/$FILENAME"
fi
rm -f $LOCK_DIR/$$
trap - INT TERM EXIT KILL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment