Building Rock Solid Websites



Image file size mogrify

What if you are given 3GB worth of .PSD files (photoshop) for images to use as product images for an ecommerce website? The images are on average 1.4 MB each. You need to get them to the under 30kb range, in order to have smooth loading for the pages. How to do this?

If you are fortunate enough to have the ImageMagick libraries loaded on your LAMP webserver, then all of this functionality is available to you right now.

You will need SSH access to your webserver also. And confidence with dealing with the command line.

mogrify is the application that can make your life simple...but whilst there is stacks of information on how to use Mogrify, it is not specific to task. So that is why I am blogging this.
So, without further ado:

I'll assume you've logged into your server using SSH.
cd /path/to/directory/full/of/psd/files
mogrify -format jpg *.psd
# the above converts psd files to jpg. If they are cmyk psd then you will end up with 3 or more jpgs per psd !! obviously this can take a lot of space and cpu time...
ls -la
# you may find that the JPG files are roughly the same HUGE size as the PSDs. This is because of EXIF data that is transferred from PSD to JPG
# download some of the JPGs to check them. I found with the batch that i was doing that mogrify created a *-0.jpg, a *-1.jpg, a *-2.jpg. The -1 was half the image data, the -2 was zero bytes empty, and the -0 was the actual good image.
So I needed to then do: rm *-1.jpg *-2.jpg *.psd
To remove all of the space-sapping files. Be careful not to remove any files that are valid!
Now the clincher:
mogrify -resize 600 -strip -quality 50% -density 72x72 *.jpg
This resizes to 600px (high or wide, STRIP EXIF data that PSDs contain, drop quality to 50% (bit drastic - you may want 85% or so), make sure density is at 72dpi.

Right now check that they are good, and move them into the right folder:
mv *.jpg /path/to/right/folder/