Current Location: Home> Latest Articles> Use the imageflip function to achieve bidirectional flip effect when image generation

Use the imageflip function to achieve bidirectional flip effect when image generation

gitbox 2025-05-29

In PHP, image processing functions are very powerful, and the imageflip function can help developers achieve image flip effect. The imageflip function is used to flip an image by specifying a flag, which can be flipped up and down or flipped left and right, and can even be used in combination to achieve a two-way flip effect.

1. Introduction to imageflip function

PHP's imageflip function is part of the image processing library GD. Its syntax is as follows:

 int imageflip(resource $image, int $mode);

Parameter description:

  • $image : Image resource, usually created through functions such as imagecreatefromjpeg , imagecreatefrommpng , etc.

  • $mode : Flip mode, defines the direction of flip. Its value can be:

    • IMG_FLIP_HORIZONTAL : horizontal flip (left and left)

    • IMG_FLIP_VERTICAL : vertical flip (up and down flip)

    • IMG_FLIP_BOTH : Flip on both directions (up and down + left and right)

2. Realize bidirectional flip of the image

To achieve a two-way flip effect, we need to set $mode to IMG_FLIP_BOTH . In this way, the image will be flipped horizontally and vertically at the same time.

Sample code:

Suppose we have an image stored in images/sample.jpg . The following PHP code demonstrates how to use the imageflip function to achieve the bidirectional flip effect of an image.

 <?php
// Loading pictures
$image = imagecreatefromjpeg('images/sample.jpg');

// Check whether the image is loading successfully
if (!$image) {
    die("Image loading failed!");
}

// Achieve two-way flip effect
imageflip($image, IMG_FLIP_BOTH);

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

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

3. Code explanation

  1. Loading image : Load a JPEG image using the imagecreatefromjpeg function. If the image fails to load, the script terminates with an error message.

  2. Bidirectional flip : Two-way flip of the image is achieved through imageflip($image, IMG_FLIP_BOTH) .

  3. Output image : Set the response header to Content-Type: image/jpeg , and then use imagejpeg to output the flipped image.

  4. Destroy image resources : Use imagedestroy to release image resources to prevent memory leakage.

4. Application in practice

Image flip function is often used in actual development for various image processing requirements, such as:

  • Create a mirror effect.

  • Rotate and flip functions available in the image editor.

  • Reverse image effect for specific graphic design requirements.

5. Things to note

  • Make sure that the image has been loaded successfully and the image resource is valid before using the imageflip function.

  • This function directly modifies the image resource, so there is no need to create a new image resource. The flipped image will be modified directly on the original image.