Resizing old images in WordPress

We can put it this way; this is getting rid of archive images in wordpress.

Because sometimes for heavy sites, old images are just unneeded that much. Removing them is not the solution, we often look for.

So I prepared a script to resize to smaller size and reducing quality to take lower disk space.

Free to use, here it is:

resizeImages.sh

#!/bin/bash
##################
# by Dincer Kavraal, 201507121840
# resizes images of wordpress uploads [given] directory
# i.e.
# wordpress, in standard, generates image thumbnails in ./wp-content/uploads/[YEAR]/[MONTH]/
# these images take name of the original image with size information in the name as trailing
# this removes the thumbnails, instead softlinks the thumbnails to the original and resizes the original small so that
# disk usage is lowered. though BE CAUTIOUS: this can increase your network throughput because replaces the thumbnails with the original in a sense
# if you don't know what you're doing, do it anyway :) but the responsibility remains all yours.
# e.g.
# let's have a directory of /var/www/wp-content/uploads/2015/01/ as:
# a.jpg a-200x200.jph a-400x400.jpg a-1024x768.jpg
# using the script with this directory as argument:
# ./resizeImages.sh /var/www/wp-content/uploads/2015/01/
# will result as
# a.jpg (resized), a-200x200 -> soft links to a.jpg, and all other a-*.jpg soft linked to a.jpg
# CAUTION
# needs imagemagicks convert
# check SIZE variable below, this will be irreversible if too small for your need
# you may check QUALITY value also. 60 seemed to be logical to me. though check for your need.
# this can result with higher network usage, since replaces thumbnails
# this can resize your logo and/or other important images, if they are in the uploads dir
# if will use as a cron job, recommended to set OUTPUT=0, (a few lines below)
# this will only resize JPEG files (jpg, jpeg, JPEG, JPG, Jpg, whatever), if you need gifs,pngs resized you can remove the statement 'grep -E -e [jJ][pP][eE]?[gG]$'  below. 
##################

OUTPUT=1
SIZE="200x200"
QUALITY="40"

echomsg() {
 if [[ $OUTPUT -eq 1 ]]; then
 printf "$@"
 fi
}

if ! convert_loc="$(type -p "convert")" || [ -z "$convert_loc" ]; then
 # this checks if imagemagick function convert exists in the system, thanks to Ivan Tarasov, http://stackoverflow.com/a/7522866/1463812
 echomsg "\033[0;31merror. imagemagick doesnt seem to be installed.\033[0m\n"
 exit -2
fi

if [[ $# -eq 0 ]]; then
 echomsg "\033[0;31merror. no args given.\033[0m\n"
 echomsg "\033[0;34muse as:\033[0m $0 {dir}\n"
 exit -1
fi

echomsg "\033[0;37mUsing dir $1\033[0m\n"

ls $1 | grep -v -E -e "\-[0-9]*x[0-9]*[.][^.]*$" | grep -E -e [jJ][pP][eE]?[gG]$ | while read i; do
 echomsg "."
 file="${i##*/}"
 filename="${file%.*}"
 convert "$1/$i" -resize ${SIZE}\> -quality $QUALITY "$1/$i"

 find $1/$filename\-* 2>/dev/null | while read s; do
 rm "$s" 2>/dev/null && ln -s "$1/$i" "$s" 2>/dev/null || echomsg "X"
 done || echomsg "x"

done

(hightlight by http://hilite.me )

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s