Installing ImageMagick for PHP on Ubuntu

iMagick is a wrapper class, wrapping the most common web image manipulation actions in easy to use functions, but allowing full access to iMagick’s power by issuing system calls to it’s command-line programs. There is no proper documentation for Installing ImageMagick for PHP on Ubuntu or any other LINUX based systems. Here is some simple steps for enabling ImageMagick library to work with PHP. Also there is no need to worry for MagickWand. iMagick will do everything for you.

First of all remove everything you have tried to make it work. Uninstall iMagick with the following command:

sudo apt-get remove imagemagick

Remove ImageMagick and all its dependencies with the following command:

sudo apt-get remove –auto-remove imagemagick

Purge the config file of ImageMagick with the following command:

sudo apt-get purge imagemagick

Or similarly, like this:

sudo apt-get purge –auto-remove imagemagick

Well, all your previous installation files are just gone. Now Install it purely and perfectly without any conflict or file modification. Enter the following two commands:

sudo apt-get install imagemagick
sudo apt-get install php5-imagick

That was way too easy – thanks interwebs! Remember to restart/reload your webserver..

sudo service apache2 graceful

That’s all. Check your php.ini or test with any simple script. For simple scripts to test iMagick you can refer to this post.

How To Get Current Page URL In PHP

Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can get current url in php.

Following function will do everything for you,

You can now get the current page URL using the line:

Sometimes it is needed to get the page name only. The following example shows how to do it:

Happy Coding .. 🙂

Display Unsupported Image Formats In Browser

Most of the browsers do not support most of the image file types rather than some common file types like JPG, PNG and GIF. But in most of the web projects user need to upload some most used file types like PSD, PDF ( Unsupported image formats ) etc. and they need to see what they have uploaded is perfect or not.

So here are some methods using PHP and ImageMagick module for PHP to disply a little more formats in browsers by converting them into JPG format.

Displaying the JPG file format image in browsers :

Displaying the PNG file format image in browsers :

Displaying the GIF file format image in browsers :

The functions used above imagecreatefromjpeg(), imagecreatefrompng(), imagecreatefromgif(), imagejpeg() for reading .jpg, .png and .gif files are library functions of PHP. So so need not to bother about how to make them work.

Displaying the PSD file format image in browsers :

Here the function used i.e. imagecreatefrompsd() is not predefined library function. So in order to make it work we need to include an extra class file which will read the PSD files and gice the browser a JPEG file as output. You can download the classPhpPsdReader.php here.

Displaying the TIFF file format image in browsers :

Displaying the PDF file format image in browsers :

You need to call he wrapper class of ImageMagick inorder to make it work of pdf and tiff files. Follow the installtion guide of ImageMagick Here.