Installing OptiPNG on Dreamhost
Google’s PageSpeed Insights can help you tweak your site’s accessibility by making things load faster. One way to make your website load faster is to optimize images.
Google suggests using Optipng for optimizing PNG or Portable Network Graphic files. There are a lot of ways to enable and use optipng, but I’m going to show you how to install it on your Dreamhost shared hosting server. Dreamhost doesnt automatically offer optipng, so until they do, it’s not that difficult to install it locally.
I would like to note here that since Optipng is not a persistent process, Dreamhost should have no trouble with us uploading and installing something that could facilitate faster web hosting.
You’ll need to know a little bit about using secure shell (ssh). I prefer to use putty.exe on windows. It’s even easier on mac and linux by just opening a terminal and ssh-ing into your preferred dreamhost user account.
- In your home directory, if you don’t already have it, create a folder “usr”. Inside that directory create directories “local” and “src”.
$ mkdir usr
$ cd usr
$ mkdir local src - Change directories into the ~/usr/src/ directory.
$ cd src - Wget the optipng source code:
$ wget http://prdownloads.sourceforge.net/optipng/optipng-0.7.4.tar.gz?download - You may have to rename the file:
$ mv optipng-0.7.4.tar.gz?download optipng-0.7.4.tar.gz - Unzip the code:
$ tar -xzvf optipng-0.7.4.tar.gz - Change directories into the new optipng-0.7.4 folder:
$ cd optipng-0.7.4 - Configure optipng to work locally:
$ ./configure –prefix=~/usr/local
(This may spin for a bit…) - Next, we compile:
$ make
(This may also spin for a bit…) - $ make install
(This should take very little time.)
Now that you have the binary compiled and properly installed locally, we can make it so your shell user can use the command by editing PATH.
- Open .bash_profile in nano:
$ nano -w .bash_profile - If you have a path profile existing in your .bash_profile, ~/usr/local/bin to the end. Otherwise if no path profile exists, use this:
PATH=$PATH:~/usr/local/bin - Control-X to exit nano, “Y” to save (same file name: .bash_profile).
- Reload .bash_profile:
$ . ./.bash_profile - Find the Optipng version:
optipng –version - Returns this:
OptiPNG version 0.7.4
Copyright (C) 2001-2012 Cosmin Truta and the Contributing Authors.
This program is open-source software. See LICENSE for more details.
Portions of this software are based in part on the work of:
Jean-loup Gailly and Mark Adler (zlib)
Glenn Randers-Pehrson and the PNG Development Group (libpng)
Miyasaka Masaru (BMP support)
David Koblas (GIF support)Using libpng version 1.4.12 and zlib version 1.2.7-optipng
You are now ready to use optipng.
A typical usage of opting is thus:
$ optipng <filename>
This replaces the existing file with the compressed file. If you don’t need a backup that’s fine. Some people might like the “MORE POWER” approach, by further compressing images using the option -07 thus:
$ optipng -07 <filename>
This uses a lot of computer crunch time and is sort of rude to other users on a shared hosting system. If you’re going to use this on Dreamhost or another shared server system, use it sparingly on relatively small images. Do any large images at home on your own computer.
File size for the original file (1016 x 758) is 524K. After compression with optipng, the image is 255K (51.26% decrease). Yet the file looks identical to the original.
$ optipng optical-illusion-spinning.png
I hope this works for you!