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:
- php -r "var_dump(function_exists('imagerotate'));"
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/
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:
- apt-get install imagemagick
- convert -version
- Version: ImageMagick 6.2.4 02/10/07 Q16 http://www.imagemagick.org
- Copyright: Copyright (C) 1999-2005 ImageMagick Studio LLC
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”
- // Image Path
- $image = "images/me.jpg";
- $original = "images/me-original.jpg";
- if($_POST['restore']) {
- exec("cp ".$original." ".$image);
- }
- else if($_GET['left'] == 1 || $_GET['right'] == 1 || $_POST['degree']) {
- // Rotation Degree
- if(isset($_GET['left'])) $degree = -90;
- if(isset($_GET['right'])) $degree = 90;
- if(isset($_POST['degree'])) $degree = $_POST['degree'];
- // rotate image
- if(is_numeric($degree)) exec("convert ".$image." -rotate ".$degree." ".$image);
- }
- 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
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