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.
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:
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:
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:
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 errorserror_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 codeunlink($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 imageexec("/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>
==========================
==========
==========
==========
==
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.
Comments (0)