<

Php and imagemagick for beginners

Published on
12,392 Points
6,292 Views
1 Endorsement
Last Modified:
Approved
I decide write this article for demonstrate how easy is working with php and imagemagick. What we will do in the end is simple is transform a normal color picture to a sepia picture.

But before that, what is Imagemagick?

In few words, Imagemagick is a command line program that can edit or alter things in almost kind of images.

You can see more detailed what is Imagemagick here:
http://imagemagick.org/script/index.php OR
http://www.rubblewebs.co.uk/imagemagick/basics/basics.php
 
The first thing you have to know is if you have Imagemagick installed in your server or PC (PC - if you use XAMPP).
 
1) Imagemagick installation.
(you can skip this step if you have already imagemagick installed).
 
Download the last version of Imagemagick. The latest release of Imagemagick is version 6.6.4-10: http://imagemagick.org/script/download.php
 
If you have a simple hosting service, most likely you don't have imagemagick installed and usually they will not install in the future.

If you have a VPS or your own server (usually a dedicated server), you can request to the server support that they install imagemagick.

In my personal case, when I start with Imagemagick my hosting was a VPS. I send the files to them (server support), and they installed the Imagemagick in server. I hope you have a great support in your server.  If you will do the installation alone, you can follow these links:

http://imagemagick.org/script/install-source.php
http://www.rubblewebs.co.uk/imagemagick/install/install.php
 
If you install Imagemagick for work with XAMPP, you can follow this link:
http://www.rubblewebs.co.uk/imagemagick/install/xampp.php
 

2) Let's Begin

Imagemagick have different command line tools, like convert, compare, montage etc. The most popular was the 'Convert'. 'Convert' usually is used for made lots of transformations and adjusts in pictures, like transform a picture to sepia, or black and white, more contrast, less brightness, and more advanced things, but for now we will do a simple things. So, we will use 'Convert' for our example.
 
For use Imagemagick through php, the code looks like:

<?
exec("convert image.jpg -sepia-tone 50% final_image.jpg "); 
?>

Open in new window


We will use the php function 'exec' for execute the imagemagick commands.

Inside 'exec' you can see:
the command convert.
The image that will be transform.
-sepia-tone 90% was the transformation (transform a normal picture to a sepia tone picture).
The final image will contain the image with sepia transformation.

If you try the code above, probably they don't work. Don't work because you don't tell the correct path to the 'Convert' tool. So before 'Convert' we have to insert the correct path, normally the path was "/usr/bin/". If that path was the correct the 'exec' line looks like:

exec("/usr/bin/convert image.jpg -sepia-tone 50% final_image.jpg ");

Open in new window


Probably now the code work well. If not work, no problem, we will check where 'Convert' is installed in your server. Create a new php file, and type:

<?
echo "<pre>"; 
system("type convert");
echo "</pre>"; 
?>

Open in new window


The output will be something identical to: "convert is /usr/bin/convert"
 
Now you have the correct path to access to the command 'Convert'. Now edit the code with the correct path.
 
3) The final code

Now we have all for begin. The code I will show now, is the code that i use for test the basic tools of imagemagick. The code will contain php, i magemagick and html (html is for we compare the original image vs final image).

Chose any color image that you like, rename to 'original.jpg'.
Create the folder 'image' and put inside the file 'original.jpg'.
Create the folder 'image_final', and change permission to '777'. In the end this folder will contain all transformed images created automatic by the script.
Open a new php file, the name of file can be 'sepia.php'. Copy and paste the code in snippet code in your new php file.

<?
//report all errors
error_reporting(E_ALL) ;

//the input image inside $ii variable. In this case the input image is locate in 'image' folder
$ii = "image/original.jpg";

//the ouput image inside $im variable. This is the image that will contain the transformations, and will be created automatic by the script and saved in 'image_final' folder.
$im = "image_final/sepia.jpg";

//erase the output image. Very until when you do a refresh or when change some values in the code
unlink($im);

//that is what imagemagick will do. In this case they will transform the original image to a sepia image with a ton of 90%
$cmd = "-sepia-tone 90%";

//The function 'exec' contain the correct path to 'Convert' command, contain the input image, the transformation we will do, and the output image
exec("/usr/bin/convert $ii $cmd $im ");

//Code bellow will show the result 
?>
<html>
<head>
<style type="text/css">
HTML{background-color: white;}            
</style>
</head>
<body>
<div align="center"><img src="<? echo $ii; ?>" /></div><br><br>
<div align="center"><img src="<? echo $im; ?>" /></div>
</body>
</html>

Open in new window


4) Conclusion

If you follow all steps and execute the file sepia.php, you will see something like:

sepia.php after executed
Now you can see how is simple work with php and imagemagick.

5)Useful links

The official site of imagemagick:
http://imagemagick.org

Examples of imagemagick usage:
http://www.imagemagick.org/Usage/
http://www.rubblewebs.co.uk/imagemagick/operators/part1.php
http://www.rubblewebs.co.uk/imagemagick/operators/part2.php
http://www.rubblewebs.co.uk/imagemagick/operators/part3.php

Other good references:
http://www.fmwconcepts.com/imagemagick/sharp/index.php
http://www.rpublica.net/imagemagick/

The references I use to write this article was:
http://imagemagick.org and http://www.rubblewebs.co.uk.

==========================================================
I hope this tutorial was useful for you. If you have any doubts please open a new question in imagemagick zone, or post here your comments.

The Best Regards, JC
1
Comment
0 Comments

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Join & Write a Comment

Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month