Current Location: Home> Latest Articles> Use the imageflip function to flip the image and save the original image

Use the imageflip function to flip the image and save the original image

gitbox 2025-05-19

In web development, image processing is a common requirement, and PHP provides rich image processing functions. This article will introduce in detail how to flip an image using the imageflip() function in PHP and how to save a copy of the original image.

1. What is the imageflip() function?

imageflip() is a function in PHP for image flipping. It can flip the image horizontally, vertically, or 180 degrees. The syntax of this function is as follows:

 bool imageflip ( resource $image, int $mode )

Parameter description :

  • $image : Image resource, images can be loaded through functions such as imagecreatefromjpeg() , imagecreatefrommpng() , etc.

  • $mode : Flip mode. Optional values ​​include:

    • IMG_FLIP_HORIZONTAL : Flip horizontally.

    • IMG_FLIP_VERTICAL : Flip vertically.

    • IMG_FLIP_BOTH : Flip horizontally and vertically.

2. Sample code

Here is a PHP script example that demonstrates how to load an image, flip it horizontally, and then save a copy of the original image.

 <?php
// Load the picture
$imagePath = 'path/to/your/image.jpg';
$image = imagecreatefromjpeg($imagePath);

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

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

// Save the flipped image
$flippedImagePath = 'path/to/your/flipped_image.jpg';
imagejpeg($image, $flippedImagePath);

// Save original copy of the picture
$originalImagePath = 'path/to/your/original_image_copy.jpg';
copy($imagePath, $originalImagePath);

// Release image resources
imagedestroy($image);

echo 'Image flipped and saved successfully!';
?>

3. Code parsing

  1. Loading image : Use the imagecreatefromjpeg() function to load an image in JPEG format. If it is a PNG or GIF image, you can use functions such as imagecreatefrommpng() or imagecreatefromgif() .

  2. Flip the image : Use the imageflip() function to flip the image horizontally. Different flip modes can be selected as needed, such as IMG_FLIP_VERTICAL or IMG_FLIP_BOTH .

  3. Save the flipped image : Save the flipped image as a new file through the imagejpeg() function. The save path can be changed according to actual needs.

  4. Save original copy of the image : Save the original image as a copy through the copy() function to preserve the unmodified version.

  5. Release resources : After the processing is completed, use the imagedestroy() function to free up the image resources to avoid memory leakage.

4. Process different types of pictures

If you need to work with pictures in other formats, you can use the corresponding load and save functions. For example:

5. Conclusion

Using PHP's imageflip() function for image flipping is a very simple operation. With this article, you can easily flip the image and keep the original copy of the image during the process in case it needs to be restored. Hope this example helps you!