In PHP, image processing is a common task. Using the imagerotate function from the GD library, you can easily rotate images. This function is especially useful when handling user-uploaded images. If you want to rotate an image and output it as a JPEG, this article will explain how to achieve that in detail.
imagerotate is a function provided by PHP's GD library that allows you to rotate images. The rotation is based on a specified angle and can fill the empty areas with a defined background color.
<span><span><span class="hljs-title function_ invoke__">imagerotate</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-variable">$angle</span></span><span>, </span><span><span class="hljs-variable">$bgColor</span></span><span>);
</span></span>
$image: An image resource, usually created by functions in the imagecreatefrom... family (like imagecreatefromjpeg, imagecreatefrompng, etc.).
$angle: The rotation angle in degrees. Positive values rotate clockwise, and negative values rotate counterclockwise.
$bgColor: The background color for empty areas after rotation, typically created with the imagecolorallocate function.
Next, we’ll demonstrate how to rotate an image in PHP and save it as a JPEG. Assume we have an image file named example.jpg.
First, load the image you want to rotate. Here, we use the imagecreatefromjpeg function to load a JPEG image.
<span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatefromjpeg</span></span><span>(</span><span><span class="hljs-string">'example.jpg'</span></span><span>);
</span></span>
Next, rotate the image using imagerotate. Suppose we want to rotate it 90 degrees clockwise with a white background.
<span><span><span class="hljs-variable">$rotatedImage</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagerotate</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-number">90</span></span><span>, </span><span><span class="hljs-number">0xFFFFFF</span></span><span>); </span><span><span class="hljs-comment">// White background</span></span><span>
</span></span>
Here, we specify a rotation angle of 90 degrees, with the background color white (0xFFFFFF represents the hexadecimal value for white).
After rotation, output the image as a JPEG. To maintain quality, you can specify the compression level (an integer from 0 to 100, where 100 is no compression).
<span><span><span class="hljs-title function_ invoke__">imagejpeg</span></span><span>(</span><span><span class="hljs-variable">$rotatedImage</span></span><span>, </span><span><span class="hljs-string">'rotated_example.jpg'</span></span><span>, </span><span><span class="hljs-number">90</span></span><span>); </span><span><span class="hljs-comment">// 90 is the output quality</span></span><span>
</span></span>
Finally, remember to free the image resources used in memory.
<span><span><span class="hljs-title function_ invoke__">imagedestroy</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">imagedestroy</span></span><span>(</span><span><span class="hljs-variable">$rotatedImage</span></span><span>);
</span></span>
Combining the above steps into a complete PHP script, the example is as follows:
<span><span><span class="hljs-meta"><?php</span></span><span>
<p></span>// Load the image<br>
$image = imagecreatefromjpeg('example.jpg');</p>
<p>// Rotate the image 90 degrees with a white background<br>
$rotatedImage = imagerotate($image, 90, 0xFFFFFF);</p>
<p>// Output the image as JPEG with quality 90<br>
imagejpeg($rotatedImage, 'rotated_example.jpg', 90);</p>
<p>// Free image resources<br>
imagedestroy($image);<br>
imagedestroy($rotatedImage);</p>
<p>echo "Image rotation completed and saved as rotated_example.jpg";</p>
<p>?><br>
</span>
Rotation Angle: The rotation angle must be in degrees. For example, 90 degrees rotates the image a quarter turn clockwise, 180 degrees rotates it half a turn, and -90 degrees rotates it counterclockwise.
Background Color: If the rotated image creates transparent areas, imagerotate fills them with the specified background color. You can define any color using the imagecolorallocate function.
Image Type: Although we use JPEG in this example, you can also use imagecreatefrompng or imagecreatefromgif to handle other image formats. The rotation operation itself is independent of the image format.
Using PHP's imagerotate function makes it easy to rotate images and save them as JPEG with just a few lines of code. Combined with other GD library functions, you can implement more advanced image processing tasks. Hopefully, the examples in this article will help you effectively handle image rotation in your projects.