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 (http://roshanbh.com.np)

  //get the detail of the image
  $imageinfo=getimagesize($sourceFile);
  switch($imageinfo['mime'])
  {
   //create the image according to the content type
   case "image/jpg":
   case "image/jpeg":
   case "image/pjpeg": //for IE
        $src_img=imagecreatefromjpeg("$sourceFile");
		break;
    case "image/gif":
        $src_img = imagecreatefromgif("$sourceFile");
		break;
    case "image/png":
	case "image/x-png": //for IE
        $src_img = imagecreatefrompng("$sourceFile");
		break;
  }
  //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); ?> 
rotated
<?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

    gdVersion();
    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 {
    define(‘MAGICK_PATH_FINAL’, MAGICK_PATH);
    }

    $cmd = MAGICK_PATH_FINAL . ” \”$name\” -rotate $r \”$dest\””;
    exec($cmd);
    } else {
    $imageinfo=getimagesize($sourceFile);
    switch($imageinfo[‘mime’])
    {
    case “image/jpg”:
    case “image/jpeg”:
    case “image/pjpeg”: //for IE
    $type = “jpg”;
    $src_img=imagecreatefromjpeg(“$sourceFile”);
    break;

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

    case “image/png”:
    case “image/x-png”: //for IE
    $type = “png”;
    $src_img = imagecreatefrompng(“$sourceFile”);
    break;
    }
    //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);
    }

    imagedestroy($src_img);
    imagedestroy($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>