How to rotate a image and save in folder in php?

by aniya.jaskolski , in category: PHP General , a year ago

How to rotate a image and save in folder in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dana , a year ago

@aniya.jaskolski 

To rotate an image and save it to a folder using PHP, you can use the GD library. The following example demonstrates how to do this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Path to the image file
$imagePath = 'path/to/image.jpg';

// Load the image
$image = imagecreatefromjpeg($imagePath);

// Rotate the image by a specified angle (in degrees)
$angle = 90; // Set the angle here
$rotatedImage = imagerotate($image, $angle, 0);

// Path to the folder where the rotated image will be saved
$saveFolderPath = 'path/to/save/folder/';

// Generate a new file name for the rotated image
$rotatedImageName = basename($imagePath, '.jpg') . '_rotated.jpg'; // Change the extension if needed

// Save the rotated image to the specified folder
imagejpeg($rotatedImage, $saveFolderPath . $rotatedImageName);

// Free up memory by destroying the images
imagedestroy($image);
imagedestroy($rotatedImage);


Make sure you have the GD library enabled in your PHP configuration (php.ini) and the appropriate permissions to read/write files and folders. Additionally, change the file path, folder path, and file extensions as per your requirements.