Install ImageMagick and imagick on Mac OS X Lion

If you’ve ever done any sort of image processing in your PHP applications, you’ll start to realise how limited you are when using native PHP commands such as createimagefromjpg etc. It eats your web servers memory! Nowadays, what with people carrying 5 Megapixel cameras in their phones, uploading and resizing a photo can be areal strain on resources, especially if several users on the website are doing it at the same time.

To get around this conundrum, there is a PHP library called imagick which allows you to access a program called ImageMagick. ImageMagick runs natively on the machine, and is available for Unix, Linux, Mac, and Windows, so running it shouldn’t be a problem. The only thing to take into consideration here is whether your hosting providers PHP has imagick available. If not, depending on your hosting package you may be able to SSH into your server and install it.

ImageMagick can do all sorts of crazy image editing things, and can handle pretty much whatever you throw at it! Find out more here:
http://www.imagemagick.org/script/index.php

Anyway, looks good, so let’s get cracking! If you have been following my posts you’ll see I upgraded my PHP from the old version that ships with Lion using ports. I’m also going to use ports to get ImageMagick.

sudo port install ImageMagick

This will take up to ten minutes! It gets all its dependencies, configures, and compiles from the source code specifically for your computer’s setup. Once it’s done there are several commands you can run from the terminal:

animate
compare
composite
conjure
convert
display
identify
import
mogrify
montage
stream

For more info see http://www.imagemagick.org/script/command-line-tools.php

Now that we’ve done that, we need to install the imagick PHP library that accesses it.
Depending on your luck this may work straight away for you:

sudo pecl install imagick

When it asks for the prefix, type it /opt/local.

If it fails to install, you are probably running PHP 5.4 like me. In this case, you need to tell pear to use the latest beta, which WILL compile.

sudo pear config-set preferred_state beta
sudo pecl install imagick
sudo pear config-set preferred_state stable

Last but least you should add “extension=imagick.so” to php.ini, and restart apache. Boom! now we have a very powerful tool for image manipulation!

Update: If you get this error – Please provide a path to MagickWand-config or Wand-config program.
Then – ensure “ImageMagick-devel” and NOT “ImageMagick” package is installed instead

Setting the PATH environment variable for your executables on Mac OS X

Sure, you can cd /wherever/your/program/lies, then run ./program_name, but thats lame!

I just installed PEAR and PECL in work, and it went int /usr/local/pear. Pear itself was in /usr/local/pear/bin, so thats the path we need to add.

From your terminal:

cd ~
nano .profile

Once Nano opens (or vi, emacs, whatever you like using as your command line text editor), add the following, and thats it!

export PATH=/path/to/your/bin:/maybe/more/folders:/one/more/for/good/measure:$PATH

As you can see, you can add several paths in the one line, just separate them by a :

Remember to quit and reload your terminal!

Upgrade to PHP 5.4 on Mac OS X Lion

The PHP that ships with Mac is getting rusty. Time to get PHP 5.4!

I tried several methods, and several fail failed. This one (https://gist.github.com/2721719) didn’t.

sudo port selfupdate
sudo port -u upgrade outdated

sudo port install php54 php54-apache2handler
cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n php5 mod_php54.so
sudo port install php54-curl php54-ftp php54-iconv php54-mbstring php54-mcrypt php54-mysql php54-openssl php54-soap php54-sqlite php54-xsl php54-zip php54-xdebug php54-mongo

sudo port install php53 php53-apache2handler
sudo port install php53-curl php53-ftp php53-iconv php53-mbstring php53-mcrypt php53-mysql php53-openssl php53-soap php53-sqlite php53-xsl php53-zip php53-xdebug php53-mongo

cd #
curl http://pear.php.net/go-pear.phar -o go-pear.phar
sudo php go-pear.phar

And then we can select which php the system uses by saying either of these:

sudo port select php php54
sudo port select php php53

When installing Pear:
Option 1. Installation base ($prefix) : /opt/local/lib/php54
4. Binaries directory : /opt/local/bin
Then get PHPUnit

pear info pear && rm go-pear.phar
sudo pear config-set auto_discover 1; sudo pear install pear.phpunit.de/PHPUnit

Lastly we nano /etc/apache2/httpd.conf and ctrl-w to look where php is. Comment the 5.3 one, and add the line:

#LoadModule php5_module libexec/apache2/lib_php5.so
LoadModule php5_module /opt/local/apache2/modules/mod_php54.so

So we can switch between the two by moving the comment. Finally restart apache!

sudo apachectl restart

phpinfo() and php -v both showing 5.4! Fantastico! Remember to set your default timezone now!

Installing PEAR on XAMPP for Mac OS X Lion

I’m preparing to install Doctrine in order to take care of Object Relational Management within my Zend Framework projects. I use XAMPP from Apache Friends, it’s nice and simple to get up and running on pretty much every platform.

If like me you use linux quite a lot, you’ll be used to some of the commands at your disposal. OS X comes with a number of these, but it annoys me when ones I frequently use are missing. One such command is wget. Now I know we can use curl -O to the same effect, so first up I edited ~/.bash_profile and added:

alias wget='curl -O'

Now when I type wget, it will run curl, so that’s one less pain in the ass. Anyway, I’m digressing.

wget http://pear.php.net/go-pear.phar

The next thing is, Mac comes with a php installed. if you type which php, it points to /usr/bin/php. We’re going to rename it, and soft link the xampp binary instead:

mv /usr/bin/php /usr/bin/php_orig
ln -s /Application/XAMPP/xamppfiles/bin/php /usr/bin/php

A quick

php --info

should tell you from pathnames etc that you are now running from your XAMPP binary. Now we can use that php command and say:

php -d detect_unicode=0 go-pear.phar

Last thing we do is create a soft link to your /usr/bin folder

ln -s /Applications/XAMPP/xamppfiles/bin/pear /usr/bin/pear

And thats us! we are now running the XAMPP php from the command line and pear is installed and configured ready to rock! Typing in pear should show you a bunch of options!

Tune in for more! Next Stop – Doctrine!