Current Location: Home> Latest Articles> How to implement horizontal mirror flip with imageflip()

How to implement horizontal mirror flip with imageflip()

M66 2025-06-03

In PHP, processing images is very powerful. By using PHP's GD library, we can perform various image processing operations such as cropping, scaling, rotation, and mirroring. Today, we will focus on how to use PHP's imageflip() function to achieve horizontal image flip of images.

Introduction to imageflip() function

The imageflip() function is a very practical function in the PHP GD library for flipping images. The basic usage of this function is to flip the direction of the image, including horizontal flip, vertical flip, or a combination of both.

Function prototype:

 bool imageflip ( resource $image, int $mode )
  • $image : Image resource, usually an image resource created through functions such as imagecreatefromjpeg() , imagecreatefrommpng() , etc.

  • $mode : Specifies the flipped mode. It can be one of several constants:

    • IMG_FLIP_HORIZONTAL : Flip horizontally.

    • IMG_FLIP_VERTICAL : Flip vertically.

    • IMG_FLIP_BOTH : Flip both horizontally and vertically.

How to achieve horizontal mirror flip

To achieve horizontal mirror flip of the image, we only need to use IMG_FLIP_HORIZONTAL mode. Here is a specific example code showing how to read an image file and flip horizontally using the imageflip() function:

Sample code:

 <?php
// Loading image files
$imagePath = 'path_to_your_image.jpg'; // Image file path
$image = imagecreatefromjpeg($imagePath); // Create image resources through file path

// Check if the image is loaded successfully
if (!$image) {
    die('Unable to load the picture');
}

// Flip the image horizontally
imageflip($image, IMG_FLIP_HORIZONTAL);

// Set content type header
header('Content-Type: image/jpeg');

// Output the flipped image to the browser
imagejpeg($image);

// Release image resources
imagedestroy($image);
?>

Code parsing

  1. Loading the image : First, we use the imagecreatefromjpeg() function to load the image from the specified path. If it is a PNG image, you can use imagecreatefrommpng() and so on.

  2. Flip the image horizontally : Use the imageflip() function and pass in the IMG_FLIP_HORIZONTAL constant to achieve horizontal mirror flip of the image.

  3. Output image : Use imagejpeg() to output the flipped image to the browser to ensure that the browser can display the processed image.

  4. Resource release : Release image resources through imagedestroy() to prevent memory leakage.

Things to note

  • When using the imageflip() function, the image resource must have been loaded successfully. Otherwise, calling imageflip() will return false and the image cannot be processed.

  • This function supports common image formats such as JPEG, PNG, and GIF to ensure that the image format you are using is compatible with the function.

  • When using it, you can replace the image path and type as needed. Make sure the image file exists and the path is correct.

Replace the domain name of the URL

If a URL is used in the code or page and needs to be replaced with its domain name m66.net , it can be done by a simple string replacement operation: