This 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.
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
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.
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
Now it’s time to display the image. We first tell the browser the content type (i.e. 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
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).
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:
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: