Rotating Images with PHP
Published 2 years, 3 months ago in Image Handling and PHPThis tutorial will show you how to rotate an image in your PHP scripts. You'll need to have the GD library installed to be able to use some of the functions we use in this guide, though this is included with most PHP installations so there's a chance you'll have it.
Image rotation in PHP is handled by a function called imagerotate(), which takes three parameters (and optionally a fourth, which deals with transparency and is beyond the scope of this guide). The first is the resource identifier which you obtain when you load the original image within your script, the second is the angle which you want to rotate the image and the third parameter is used when the image isn't going to be rectangular, and specifies the colour with which to fill the uncovered areas of the rotated image to make it into a rectangle (since all image formats require rectangular images).
The first thing we need to do is to load the original image into the script, we do this with a command such as imagecreatefromjpeg("file.jpg") for JPEG images, or imagecreatefrompng("file.png") for PNG images.
-
// filename of the original image
-
$fileName = "cow.jpg";
-
// load the original image from the file
-
$original = imagecreatefromjpeg($fileName);
In this example, we're using an image called cow.jpg. The method imagecreatefromjpeg() returns a resource identifier which represents that image within the script - we store this in the variable $original. We need to pass this as the first parameter of the imagerotate() method.
The original image we're using is this particularly nice cow:

Now it's time to rotate the image! First we have to decide how many degrees to rotate the image anti-clockwise. Firstly, we'll just roate the image 90 degrees - this means that the resulting image will be rectangular and we won't have to worry about the third parameter.
-
// degrees to rotate the image (counter clockwise)
-
$angle = 90.0;
-
// rotate the image by $angle degrees
-
$rotated = imagerotate($original, $angle, 0);
We pass the original image identifer - $original - and the angle to rotate by - $angle - into the imagerotate() function. In this special case the third parameter isn't important so we'll just enter 0 (which represents the colour black). This returns an identifier for the new rotated image, which we store in the variable $rotated.
Now it's time to display the image. We first tell the browser the content type (IE what sort of image we're going to display) then use the appropriate command to output the data of the image. We're dealing with JPEG images in this example, so the Content-type is "image/jpeg" and the method to output the image is imagejpeg().
-
// print the appropriate header type
-
// this tells the browser we're displaying a jpeg image
-
// use the imagejpeg() method to display the rotated image
-
imagejpeg($rotated);
That code tells the browser we're about to display a jpeg image, then we call imagejpeg($rotated) which turns the image identified by $rotated into the a JPEG image for the browser to display. The output of this code is a sideways cow:

Now let's consider the case where you want to rotate an image by an angle such as 45 degrees which requires the imagerotate() method to fill around the rotated image to produce a rectangle. We need to pass an integer as the third parameter to tell the method which colour to fill around the image with. We will do this by specifying the hexidecimal representation of the colour, as is used in HTML. For example, FF0000 is red (each digit is between 0-F; the first two digits represent the red component, the next two are green and the last two are blue) - in HTML we would use #FF0000. To tell PHP that we're using hex to represent the numbers rather than decimal, we must prefix this number by 0x (zero x).
-
// degrees to rotate the image (counter clockwise)
-
$angle = 45.0;
-
// if the resulting image is not rectangular..
-
// .. what colour will the uncovered bits be?
-
$bgColour = 0xFF0000; // red
-
// rotate the image by $angle degrees
-
$rotated = imagerotate($original, $angle, $bgColour);
The imagerotate() method works as with a 90 degree rotation except fills the outsides with the colour red. The resulting output is:

Other hexidecimal colour codes are:
- 0xFFFFFF - white
- 0x00FF00 - green
- 0x000000 - black
That's it! This tutorial should have helped you to rotate images in PHP.
The full PHP code for the 45 degree example is below:
-
<?
-
// filename of the original image
-
$fileName = "cow.jpg";
-
-
// degrees to rotate the image (counter clockwise)
-
$angle = 45.0;
-
-
// if the resulting image is not rectangular..
-
// .. what colour will the uncovered bits be?
-
$bgColour = 0xFFFFFF; // red
-
-
// load the original image from the file
-
$original = imagecreatefromjpeg($fileName);
-
-
// rotate the image by $angle degrees
-
$rotated = imagerotate($original, $angle, $bgColour);
-
-
// print the appropriate header type
-
// this tells the browser we're displaying a jpeg image
-
// use the imagejpeg() method to display the rotated image
-
imagejpeg($rotated);
-
?>
If you have a website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!
8 Responses to “Rotating Images with PHP”
- 1 Trackback on Jul 22nd, 2006 at 9:02 am
Leave a Reply
You must log in to post a comment.
Search
Categories
- ASP (3)
- ASP Basics (3)
- Blogs (2)
- C# (1)
- deals (1)
- Firefox (1)
- Flash (1)
- Google (6)
- Hacks (4)
- Java (1)
- PHP (12)
- Basics (8)
- Files (1)
- Image Handling (3)
- Puzzles (1)
- Ruby (1)
- Tips (10)
- Web Hosting (3)
- Web Hosting Articles (1)
- Web Hosting News (2)
- Windows (6)
- Wordpress (2)
Related Entries
- PHP
- How to use PHP in pages with a .html extension (or any other)
- Generating Random Numbers in PHP
- Finding Items in an Array with PHP
- The Difference Between require() and include()
- Find a visitor's IP Address with PHP
- How to Find the Current URL with PHP
- How to use the query string in PHP
- Reading a file's contents with PHP
- Write text on a dynamically generated image using PHP
very nice. 10x to help !
thanx for all your help
actually i want to make a photogallery of mine for my site
so I decided to provide all facility but doing all by own
now just copying or downloading the code
it really helped me..
May God Bless You . But Plz Tell me what if the image i want to rotate is above another image ? i.e. overlapping images ?
Does this only work with JPEG?
O, and people, if you use other then Truetype images you should first transform to truetype (read that somewhere else).
I dont think that it only works with jpeg. i first convert jpeg to png and then rotated it !
Yaa i used imagetruecolortopalette() & imagecolortransparent() functions after roating it !
Awesome example. Very simple to understand.
—————————————————–
To save your image as a file, simply use this command:
imagejpeg($rotated, $newfile, $quality);
// $rotated = image in memory
// $newfile = the filename of the file you want to create
// $quality (1.00 = 100%, .8 = 80%) - This controls the compression of JPGs
————————————————————————
PS: Just like Sanjay, I’m making a photo gallery. Check it out @ http://www.animejutsu.com/
Sincerely,
-Gordon
-7/29/07