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.


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.

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).

You can try a live demo of the application here:


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

To make sure installation is alright, run the following command from the shell:

convert -version

You should see something like this:

Version: ImageMagick 6.2.4 02/10/07 Q16 http://www.imagemagick.org
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”


  // 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);

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:
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.


  1. Pingback: XkiD | Facebook type image rotation and more using PHP and Javascript | blog.xkid.ro

  2. Pingback: Facebook type image rotation and more using PHP and Javascript …

    1. Pingback: Twitted by charleslau

    2. Pingback: 25 New & Useful PHP Techniques & Tutorials

    1. Pingback: 25 New & Useful PHP Techniques & Tutorials | Web Development News

  3. Pingback: 35 Really Useful PHP Tutorials And Development Techniques | Smashing Buzz

  4. Pingback: 22 Useful PHP Tips and Tutorials for Web Development | Vector N Pixel

  5. Pingback: 25 New & Useful PHP Techniques & Tutorials « PIXEL SHOP

  6. Pingback: 30 Useful PHP Tutorials « Web Creation in Cambodia

  7. Pingback: Web Creation in Phnom Penh » 30 Useful PHP Tutorials

  8. Pingback: 25 Useful PHP Techniques and Tutorials | Timesneed.com

  9. Pingback: 25 New & Useful PHP Techniques & Tutorials | DevIphoneApp

  10. Pingback: Amy N Boon » 25 New & Useful PHP Techniques & Tutorials

  11. Hi Admin Help Me For Removing Error from This Code It is Basically a javascript code for Moving Text in webpage.

    Code is:

    Moving Text

    div # one

    var x,y;
    function fun()
    set interval(“fun()”,50);
    function fun1()



  12. Pingback: 25 Nya och Användbara PHP Tekniker & handledningar | Artiklar

  13. Pingback: 25 New & Useful PHP Techniques & Tutorials | Certalinx

Leave a Reply