Some people allow their website users to upload photos. This may be to share with forum members, to use as avatars, or for other reasons. It's nice to give them the option to rotate the image, as it may not be oriented that way they would like. This code shows you how to rotate an image using PHP.
Time Required: Fast
//Your image, or your user's image that you want to rotate
$image = 'myfile.jpg';
//Number of degrees you wish to rotate the image, this could be a drop down value $degrees = 90;
// This sets the image type to .jpg but can be changed to png or gif
header('Content-type: image/jpeg') ;
// Makes the Canvas
$source = imagecreatefromjpeg($image) ;
// Rotates the image
$rotate = imagerotate($source, $degrees, 0) ;
// Outputs the new jpg image, you could change this to gif or png if needed
- This code can be used as a base for all images that you need to rotate on your website. In this example we rotate a JPG image, however a GIF or PNG can also be used. Do this by changing imagejpeg to imagegif or imagepng.