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.
// 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
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.