1. Computing

Discuss in my forum

Rotate an Image

Using PHP and the GD Library

By , About.com Guide

Rotate an Image

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.
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. PHP Functions
  5. Rotate Image PHP - GD Library Rotate Image - Rotate Photo with PHP and the GD Library

©2013 About.com. All rights reserved.