How to Rotate an Image using PHP
It is not very common but it could be a situation that we have an image horizontally and need to display vertically. The first idea for this is to use photoshop to rotate the image but the goal of this article is to learn how to do with PHP programming.
It’s very simple. PHP has some functions in your library that help us much work.
Look at the following code:
<? php // initial horizontal image $ image = 'cutedad.jpg' ; // Destination of the new image vertically $ image_rotate = 'cutedad_rotate.jpg' ; // Define the degrees of rotation $ degrees = 90 ; // Create a new image from the initial file $ source = imagecreatefromjpeg ( $ image ) ; // We rotate the image 90 degrees $ rotate = imagerotate ( $ source , $ degrees , 0 ) ; // Create the file jpg Vertical imagejpeg ( $ rotate , $ image_rotate ) ; ?>
Where we have:
- An initial image displayed horizontally
- We define the name of the destination image
- We define the degrees of rotation. We are interested rotate 90 degrees
- We use the function imagecreatefromjpeg () to create a new image from an initial
- We rotate the image with the desired angle function imagerotate ()
- And finally created the jpg file with the image vertically with the function imagejpeg ()