This little snippet is a script I have running via Cron, set to execute every 30 minutes. If like me, you have maybe running forum software that isnt so great handling image uploads in posts and find that users complain because they themselves arent too sure how to resize images prior to posting then this may be useful.
What it does is scan a specified folder every 30 minutes(as set by cron) for new uploaded jpg’s that are greater than a specified size. Using the ImageMagick libraries, it then applies compression and resizes proportionally the image if it exceeds a specified height & width. Images which have been uploaded as 2Mb originals have been processed and saved as 20k files, thats 1% of the original size without any real noticable difference in quality. People tend to upload images which are suitable for professional printing and far exceed the quality required for simple website viewing.
This is my code:
#!/bin/bash src_dir="/var/www/ukordinance/images/fbfiles/images" DATETIME=`date +%d-%m-%Y-%H:%M` echo "Looking for files in : $src_dir on $DATETIME" #find $src_dir -iname '*.jp*g' -size +100k -mmin -30 for i in `find $src_dir -iname '*.jp*g' -size +256k` ; do echo "Processing : "$i convert -resize 1024x768\> -quality 80 $i $i; done