In PHP, we often need to process images. Fortunately, PHP provides a very simple function imageflip() that can be used to flip images. Not just flip horizontally or vertically, it also makes it easy to achieve 180 degrees of rotation.
imageflip() is an image processing function in PHP that flips images. Its functions include:
Horizontal flip
Vertical flip
180 degrees flip
The syntax of this function is as follows:
imageflip(resource $image, int $mode): bool
$image : Image resource that needs to be flipped.
$mode : flip mode, the specific value can be:
IMG_FLIP_HORIZONTAL (Horizontal Flip)
IMG_FLIP_VERTICAL (Vertical Flip)
IMG_FLIP_BOTH (180 degrees flip)
If you want to rotate the image 180 degrees, using the imageflip() function is very simple, just pass IMG_FLIP_BOTH as the second parameter.
The sample code is as follows:
<?php
// Loading the image
$image = imagecreatefromjpeg('example.jpg');
// conduct180Flip
imageflip($image, IMG_FLIP_BOTH);
// Save the flipped image
imagejpeg($image, 'rotated_image.jpg');
// Free memory
imagedestroy($image);
?>
Loading the image : We first use the imagecreatefromjpeg() function to load the JPEG image to be processed. You can replace loading functions in other image formats (such as imagecreatefrommpng() or imagecreatefromgif() ) based on the actual situation.
Perform 180-degree flip : Call imageflip() function and pass in the IMG_FLIP_BOTH parameter to achieve 180-degree flip of the image.
Save the flipped image : Use the imagejpeg() function to save the flipped image to a new file. You can choose different image saving formats as you want.
Free memory : Use imagedestroy() to release image resources to prevent memory leakage.
Using the imageflip() function, PHP makes image processing very simple. Just one line of code is required to achieve 180-degree rotation of the image. Whether it is used for image flipping or other common image processing tasks, imageflip() is a very practical tool.