Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to rotate an image with a given angle using imagerotate() function in PHP?
The imagerotate() function is an inbuilt PHP function used to rotate an image by a specified angle in degrees. This function is part of the GD extension and provides a simple way to perform image rotation operations.
Note: This function requires the GD extension to be enabled. Install it using:
sudo apt-get install php-gd(Ubuntu/Debian) or enable it in php.ini by uncommentingextension=gd.
Syntax
resource imagerotate($image, $angle, $bgd_color, $ignore_transparent = 0)
Parameters
The imagerotate() function accepts four parameters ?
$image − An image resource created by functions like imagecreatetruecolor(), imagecreatefromjpeg(), etc.
$angle − Rotation angle in degrees. Positive values rotate counterclockwise, negative values rotate clockwise.
$bgd_color − Background color for uncovered areas after rotation. Use imagecolorallocate() to create the color.
$ignore_transparent − Optional parameter. If non-zero, transparent colors are ignored (default is 0).
Return Value
Returns an image resource on success, or FALSE on failure.
Example 1: 90-Degree Rotation
<?php
// Create a sample image
$image = imagecreatefromjpeg('/path/to/your/image.jpg');
// Create background color (black)
$bgColor = imagecolorallocate($image, 0, 0, 0);
// Rotate image 90 degrees counterclockwise
$rotatedImage = imagerotate($image, 90, $bgColor);
// Output image
header("Content-type: image/jpeg");
imagejpeg($rotatedImage);
// Clean up memory
imagedestroy($image);
imagedestroy($rotatedImage);
?>
Example 2: 180-Degree Rotation
<?php
// Load image
$image = imagecreatefromjpeg('/path/to/your/image.jpg');
// Create white background color
$bgColor = imagecolorallocate($image, 255, 255, 255);
// Rotate image 180 degrees
$rotatedImage = imagerotate($image, 180, $bgColor);
// Save to file instead of outputting
imagejpeg($rotatedImage, '/path/to/output/rotated_image.jpg', 90);
echo "Image rotated and saved successfully!";
// Clean up
imagedestroy($image);
imagedestroy($rotatedImage);
?>
Example 3: Custom Angle with Transparent Background
<?php
// Create a test image
$image = imagecreatetruecolor(200, 200);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
// Draw a rectangle
imagefill($image, 0, 0, $white);
imagerectangle($image, 50, 50, 150, 150, $black);
// Create transparent background for rotation
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
// Rotate 45 degrees
$rotated = imagerotate($image, 45, $transparent);
// Enable alpha blending
imagealphablending($rotated, false);
imagesavealpha($rotated, true);
// Output as PNG to preserve transparency
header("Content-type: image/png");
imagepng($rotated);
imagedestroy($image);
imagedestroy($rotated);
?>
Key Points
- Positive angles rotate counterclockwise, negative angles rotate clockwise
- Use PNG format to preserve transparency in rotated images
- Always clean up image resources with imagedestroy() to free memory
- Background color fills the empty areas created during rotation
Conclusion
The imagerotate() function provides an efficient way to rotate images in PHP. Remember to handle transparency properly and always clean up image resources to prevent memory leaks.
