Wednesday, March 3, 2010

Facebook type image rotation and more using PHP and Javascript

If you are a facebook geek like me, you must have noticed till now the image rotate functionality in the photo albums. Facebook allows you to rotate images 90 degree clockwise and anti-clockwise after image upload. If you haven’t tried that till now, below is a screenshot for your convenience.
facebook-image-rotate
Question:
But the question is how does facebook team succeed doing this in one click. Today I tried looking around for a solution over internet and I came across the inbuilt imagerotate functionality in PHP.
Problem:
Unfortunately the problem is that even if you have GD Library extension enabled in PHP, imagerotate function just doesn’t work. After some research I found that to enable imagerotate function inside PHP, you need to compile and build PHP manually and enable imagerotate while installing PHP. You can check your PHP installation support for imagerotate using the following command line:
  1. php -r "var_dump(function_exists('imagerotate'));"  
Solution:
Since I didn’t want to touch my current installation of PHP, ImageMagick came to my rescue. Below I will show you how did I achieve cloning facebook’s 90 degree rotation functionality and also added a custom degree rotation (functionality usually seen in collage tools).
Demo:
You can try a live demo of the application here:
http://abhinavsingh.com/webdemos/imagerotate/
facebook-image-rotate-using-php-javascript
Source Code:
Before you go ahead and try this demo, you will need to install imagemagick on your system. On debian and ubuntu this can be achieved by the following command:
  1. apt-get install imagemagick  
To make sure installation is alright, run the following command from the shell:
  1. convert -version  
You should see something like this:
  1. Version: ImageMagick 6.2.4 02/10/07 Q16 http://www.imagemagick.org  
  2. Copyright: Copyright (C) 1999-2005 ImageMagick Studio LLC  
Convert is a command line utility provided by imagemagick. Read more about convert here.
The source code consists of the following files and folders:
  • index.php : Generates the Frontend UI part for the application
  • action.php : Responsible for handling requests and rotating image using imagemagick library
  • style.css : Used for styling the UI
  • script.js : Used for handling the horizontal slider
  • images : This folder contains all the required images for the application. The image which we will be rotating left and right is “me.jpg”
action.php
  1. // Image Path  
  2. $image = "images/me.jpg";  
  3. $original = "images/me-original.jpg";  
  4.   
  5. if($_POST['restore']) {  
  6.   exec("cp ".$original." ".$image);  
  7. }  
  8. else if($_GET['left'] == 1 || $_GET['right'] == 1 || $_POST['degree']) {  
  9.   // Rotation Degree  
  10.   if(isset($_GET['left'])) $degree = -90;  
  11.   if(isset($_GET['right'])) $degree = 90;  
  12.   if(isset($_POST['degree'])) $degree = $_POST['degree'];  
  13.   
  14.   // rotate image  
  15.   if(is_numeric($degree)) exec("convert ".$image." -rotate ".$degree." ".$image);  
  16. }  
From the UI, a user can pass 4 kind of requests:
  • Rotate Image 90 degree clockwise by clicking the button on bottom right corner
  • Rotate Image 90 degree anti-clockwise by clicking the button on bottom right corner
  • Rotate Image x degree, by choosing the value of x using horizontal slider on bottom left
  • Restore the image back to the original self
action.php handles the incoming 4 cases, calculates $degree to rotate and passes as a parameters to the command line utility provided by imagemagick.
Download Code:
Download the complete source code from here:
http://abhinavsingh.googlecode.com/files/image-rotate-application.rar
Unzip into a folder, and make the images directory writable.
PS: Application not tested on IE browser
Don’t forget to share and leave a comment if you liked the post.
Cheers!

No comments:

Post a Comment