1. Home
  2. Computing & Technology
  3. PHP / MySQL

Rotate an Image
Using PHP and the GD Library

By Angela Bradley, About.com

Image rotated 180 degrees

Angela Bradley
Using the GD Library we can manipulate images in PHP. This script uses the imagerotate () function to rotate a photo. In our case we are rotating it 180 degrees but this can be changed in the script.
<?php
// The file you are rotating
$image = 'myfile.jpg';

//How many degrees you wish to rotate
$degrees = 180;

// This sets the image type to .jpg but can be changed to png or gif
header('Content-type: image/jpeg') ;

// Create the canvas
$source = imagecreatefromjpeg($image) ;

// Rotates the image
$rotate = imagerotate($source, $degrees, 0) ;

// Outputs a jpg image, you could change this to gif or png if needed
imagejpeg($rotate) ;
?>
Each line in the code is explained with comments. The most important part of this script, the rotating, is done with the imagerotate() function. The parameters of this function are: imagerotate (The_image, degrees_to_rotate, background_color, Optional_ignore_transparency). If the optional ignore transparency is blank or 0, then the transparency is preserved.
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP Functions
  5. Rotate Image PHP - GD Library Rotate Image - Rotate Photo with PHP and the GD Library

©2009 About.com, a part of The New York Times Company.

All rights reserved.