In PHP, image processing features are very powerful, and the imagerotate function is one of the tools used for rotating images. This function lets you rotate an image clockwise or counterclockwise by a specified angle and supports multiple image formats such as PNG, JPEG, and GIF.
The basic syntax of the imagerotate function is as follows:
resource imagerotate ( resource $image , float $angle , int $bgd_color )
$image: The original image resource, usually created through functions like imagecreatefromjpeg(), imagecreatefrompng(), etc.
$angle: The rotation angle in degrees. A positive value rotates the image clockwise; a negative value rotates it counterclockwise.
$bgd_color: The background color of the image after rotation, typically defined using the imagecolorallocate() function.
To rotate an image clockwise, you need to pass a positive angle to the imagerotate function. Below is a sample code demonstrating how to rotate an image 90 degrees clockwise using imagerotate:
<?php
// Load the image
$image = imagecreatefromjpeg('example.jpg');
<p>// Set rotation angle to 90 degrees (clockwise)<br>
$angle = 90;</p>
<p>// Set background color (you can choose white, black, or other colors)<br>
$bgd_color = imagecolorallocate($image, 255, 255, 255); // White background</p>
<p>// Perform the rotation<br>
$rotated_image = imagerotate($image, $angle, $bgd_color);</p>
<p>// Save the rotated image<br>
imagejpeg($rotated_image, 'rotated_example.jpg');</p>
<p>// Free image resources<br>
imagedestroy($image);<br>
imagedestroy($rotated_image);</p>
<p>echo "Image has been successfully rotated and saved.";<br>
?><br>
imagecreatefromjpeg(): Loads the original JPEG image. You can also load other formats using functions like imagecreatefrompng() or imagecreatefromgif() as needed.
imagecolorallocate(): Sets the background color. In this example, white (RGB 255, 255, 255) is used. This background color will appear in the empty areas after rotation.
imagerotate(): Performs the actual rotation. The first argument is the loaded image, the second is the rotation angle, and the third is the background color.
imagejpeg(): Saves the rotated image. If you're working with PNG or GIF formats, you can use imagepng() or imagegif() instead.
imagedestroy(): Frees the image resources to avoid memory leaks.
When rotating an image, the imagerotate function creates a new image resource and returns it, so you need to handle and save this new image.
The choice of background color is important, especially when the rotation angle is not a multiple of 90 degrees, as blank areas will appear with the specified background color.
If you need to work with images that have transparent backgrounds (such as PNGs), make sure to use a transparent color as the background. You can set a transparent background with the following code:
$bgd_color = imagecolorallocatealpha($image, 0, 0, 0, 127); // Fully transparent background
Using the imagerotate function, you can easily rotate images by specified angles, including clockwise rotations. It is important to set an appropriate background color during rotation, especially when dealing with images that have transparent backgrounds. This makes the imagerotate function very flexible and practical for image rotation tasks.