Function to rotate image in PHP

GD library in PHP is very useful for image processing and you can do a lot image manipulation from it. In this post, I’ll show you a simple Image manipulation (image rotation)using the function provided below in PHP. You’ll see how easy it is to rotate an image using PHP.



Function to rotate image using GD library of PHP

function rotateImage($sourceFile,$destImageName,$degreeOfRotation)
  //function to rotate an image in PHP
  //developed by Roshan Bhattara (

  //get the detail of the image
   //create the image according to the content type
   case "image/jpg":
   case "image/jpeg":
   case "image/pjpeg": //for IE
    case "image/gif":
        $src_img = imagecreatefromgif("$sourceFile");
    case "image/png":
	case "image/x-png": //for IE
        $src_img = imagecreatefrompng("$sourceFile");
  //rotate the image according to the spcified degree
  $src_img = imagerotate($src_img, $degreeOfRotation, 0);
  //output the image to a file
  imagejpeg ($src_img,$destImageName);

The above function takes takes three argument, first one is the source image to be rotated and the second one is the name of file which is resulted after rotating the original image. And, the last parameter is the degree of the rotation of Image.

In this PHP function, first of all the information about the source image is stored in “$imageinfo” array. The MIME type of the file are stored in “mime” key of the “$imageinfo” array. And then appropriate image resource is created using the proper MIME type. And then, imagerotate() function of PHP is used for the rotation of the image then imagejpeg() is used to output the image to a file.

Example of the roation of Image using PHP function

Look at the original Image which I’m going to rotate in PHP

Now let’s look at the php code to call the above function see the result of rotated image with that function

rotation of image
 <?php rotateImage('girl.jpg','rotated.jpg',90); ?> 
<?php rotateImage('girl.jpg','rotated.jpg',-45); ?> 

3 thoughts on “Function to rotate image in PHP

  1. Jules

    Thanks for this tip.
    I would like to remind everyone that this process reduces the quality of the picture.
    File 1 = 36.38 kB (37248 bytes)
    File 2 = 7.56 kB (7746 bytes)
    what a pity!

  2. y2k2000

    Ty for the script.
    Also I made some improvements that i wish share with you:
    – Fix GD version >= 3
    – Fix imagejpeg() : output according to original image format

    if ($gd >= 3) {
    $r = -$degreeOfRotation;
    if (!defined(‘MAGICK_PATH’)) {
    define(‘MAGICK_PATH_FINAL’, ‘convert’);
    } else if (strpos(strtolower(MAGICK_PATH), ‘c:\\’) !== false) {
    define(‘MAGICK_PATH_FINAL’, ‘”‘ . MAGICK_PATH . ‘”‘);
    } else {

    $cmd = MAGICK_PATH_FINAL . ” \”$name\” -rotate $r \”$dest\””;
    } else {
    case “image/jpg”:
    case “image/jpeg”:
    case “image/pjpeg”: //for IE
    $type = “jpg”;

    case “image/gif”:
    $type = “gif”;
    $src_img = imagecreatefromgif(“$sourceFile”);

    case “image/png”:
    case “image/x-png”: //for IE
    $type = “png”;
    $src_img = imagecreatefrompng(“$sourceFile”);
    //rotate the image according to the spcified degree
    $src_img = imagerotate($src_img, $degreeOfRotation, 0);

    //output the image to a file
    if ($type == ‘png’) {
    imagepng($src_img, $destImageName);
    } elseif ($type == ‘gif’) {
    imagegif($src_img, $destImageName);
    } else {
    imagejpeg($src_img, $destImageName);





  3. The rotation is not working in IE. The image is getting freeze. Do not let me know how to solve this issue.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>