Current Location: Home> Latest Articles> IMG_FLIP_BOTH How to achieve simultaneous horizontal and vertical flip

IMG_FLIP_BOTH How to achieve simultaneous horizontal and vertical flip

M66 2025-05-31

In PHP, we can use the imageflip function to flip the image. This function allows us to flip horizontally, vertically, or both at the same time. This article will introduce how to use this function to achieve horizontal and vertical flip of the picture.

Imageflip function introduction

imageflip is an image processing function that performs image flipping. It has a simple syntax as follows:

 bool imageflip ( resource $image, int $mode )
  • $image : An image resource that can be created through functions such as imagecreatefromjpeg() , imagecreatefrommpng() , etc.

  • $mode : Specifies the type of flip. It can be one of the following constants:

    • IMG_FLIP_HORIZONTAL : Horizontal flip

    • IMG_FLIP_VERTICAL : vertical flip

    • IMG_FLIP_BOTH : Perform horizontal and vertical flips simultaneously

Achieve simultaneous horizontal and vertical flips

To flip horizontally and vertically at the same time, we simply set the $mode parameter to IMG_FLIP_BOTH . Here is a complete code example to achieve this:

 <?php
// Loading image files
$imagePath = "https://m66.net/images/sample.jpg";
$image = imagecreatefromjpeg($imagePath);

// Check whether the image is loading successfully
if ($image === false) {
    die("Unable to load the picture!\n");
}

// Perform horizontal and vertical flips
imageflip($image, IMG_FLIP_BOTH);

// Output the flipped image
header('Content-Type: image/jpeg');
imagejpeg($image);

// Clean the memory
imagedestroy($image);
?>

Code parsing

  1. Loading the image : Use the imagecreatefromjpeg() function to load the image. If your image is in PNG or GIF format, you can use imagecreatefrommpng() or imagecreatefromgif() respectively to load it.

     $image = imagecreatefromjpeg($imagePath);
    
  2. Check if the image is loaded successfully : If the image fails to load (such as a path error or the file does not exist), imagecreatefromjpeg() returns false . We use if ($image === false) to perform error checking.

  3. Perform flip : Use the imageflip() function to flip, passing the IMG_FLIP_BOTH constant to perform horizontal and vertical flips simultaneously.

     imageflip($image, IMG_FLIP_BOTH);
    
  4. Output the flipped image : Use the imagejpeg() function to output the flipped image to the browser. Set the correct MIME type (in this example, JPEG) through the header() function to ensure that the browser displays the image correctly.

  5. Clean memory : After processing the image, call the imagedestroy() function to release the image resource.

     imagedestroy($image);
    

in conclusion

By using the imageflip function, we can easily flip the image horizontally, vertically, or both. This flip function is very useful in image processing tasks, especially when generating images dynamically. If you want to perform other types of image processing, PHP's GD library also provides more powerful image processing capabilities.

Hope this article helps you understand how to use imageflip function in PHP for image flipping. If you have any questions, please feel free to contact me.