Current Location: Home> Latest Articles> How to achieve horizontal and vertical flip switching of images through imageflip?

How to achieve horizontal and vertical flip switching of images through imageflip?

gitbox 2025-05-27

In PHP, the imageflip function is a very practical image processing function that can flip images horizontally or vertically. This is very useful for many image processing tasks, such as in scenes such as image editing, user avatar processing, etc. This article will introduce in detail how to use PHP's imageflip function to achieve horizontal flip and vertical flip of images, and discuss the skills to switch these two flip effects.

1. Overview of imageflip function

The imageflip function is a function provided by PHP's GD library, which is used to flip an image. It has the following basic syntax:

 imageflip(resource $image, int $mode): bool
  • $image : This is the image resource to be processed. You can use image createfromjpeg() , imagecreatefrommpng() and other functions to load images from the file.

  • $mode : Flip mode, use constants to specify the flip type:

    • IMG_FLIP_HORIZONTAL : Horizontal flip

    • IMG_FLIP_VERTICAL : Vertical flip

    • IMG_FLIP_BOTH : Flip horizontally and vertically simultaneously

The return value is true to indicate success, and false to indicate failure.

2. Achieve horizontal picture flip

First, let’s look at how to achieve horizontal flip of the image. Horizontal flip means that the image will be symmetrically flipped along the vertical axis, the left part will become the right, and the right part will become the left.

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

// Perform horizontal flip
imageflip($image, IMG_FLIP_HORIZONTAL);

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

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

The above code will load the image sample.jpg , flip it horizontally, and then output the flipped image. Please note that here we replaced the domain name of the URL as gitbox.net .

3. Realize vertical flip of the picture

Next, we implement vertical flip of the image. Vertical flips will cause the image to flip symmetrically along the horizontal axis, and the upper half of the image will become the lower half and the lower half will become the upper half.

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

// Perform vertical flip
imageflip($image, IMG_FLIP_VERTICAL);

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

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

This code loads the image and flips vertically, and then displays the flipped result. Similarly, the domain name has been modified to gitbox.net .

4. Flip horizontally and vertically simultaneously

If you want the image to flip horizontally and vertically at the same time, you can also use the IMG_FLIP_BOTH constant:

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

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

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

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

This code double-flips the image to create an inverted and mirrored effect.

5. Tips for switching flip effects

In practical applications, it may be necessary to switch the flip effect according to different scenarios. You can control the type of flip through conditional judgment. For example, you can decide whether to flip horizontally, vertically, or both based on user input.

 <?php
// Suppose the user chooses some way of flipping
$flip_type = 'horizontal'; // Can be 'horizontal' or 'vertical' or 'both'

// Loading pictures
$image = imagecreatefromjpeg('http://gitbox.net/images/sample.jpg');

// Judge the flip method
if ($flip_type == 'horizontal') {
    imageflip($image, IMG_FLIP_HORIZONTAL);
} elseif ($flip_type == 'vertical') {
    imageflip($image, IMG_FLIP_VERTICAL);
} elseif ($flip_type == 'both') {
    imageflip($image, IMG_FLIP_BOTH);
}

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

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

In this way, you can flexibly adjust the image flip effect according to the different flip types selected by the user.

Summarize

PHP's imageflip function provides a very simple way to achieve horizontal and vertical flips of images, and supports two flip effects at the same time. In practical applications, you can flexibly select the type of flip according to your needs and switch the flip effect through simple conditional statements. Whether it is processing images uploaded by users or performing image editing, these flip operations can improve the flexibility and efficiency of image processing.