Current Location: Home> Latest Articles> PHP scripts that implement self-time flip (mirror) effects

PHP scripts that implement self-time flip (mirror) effects

M66 2025-05-31

When developing websites or applications for photo processing, we often encounter users uploading selfies, but due to the imaging method of the front camera, the image is in a "mirror" state, and users may want to flip it into a photo from a natural perspective. At this time, we can use the imageflip() function provided by PHP to easily achieve this requirement.

This article will take you through how to use imageflip() and use a complete example to demonstrate how to handle uploaded selfies to make them mirrored horizontally.

What is imageflip?

imageflip() is a GD image library function in PHP, used to flip images horizontally, vertically, or simultaneously. The basic syntax is as follows:

 bool imageflip(GdImage $image, int $mode)

Parameter description:

  • $image : The image resource to operate on.

  • $mode : flip mode, which can be one of the following constants:

    • IMG_FLIP_HORIZONTAL : horizontal flip (mirror)

    • IMG_FLIP_VERTICAL : vertical flip

    • IMG_FLIP_BOTH : horizontal and vertical flip

Realize horizontal mirror flip of selfie photos

Here is a complete example that simulates a user's selfie photo upload, flip it horizontally and save it as a new file.

 <?php
// Simulate the upload file path(Please use it in actual applications $_FILES Get uploaded file)
$sourcePath = 'uploads/selfie.jpg';
$destinationPath = 'uploads/selfie_flipped.jpg';

// Check if the file exists
if (!file_exists($sourcePath)) {
    die('The source file does not exist,Please upload the photos and try again。');
}

// Get image information
$imageInfo = getimagesize($sourcePath);
$mime = $imageInfo['mime'];

switch ($mime) {
    case 'image/jpeg':
        $image = imagecreatefromjpeg($sourcePath);
        break;
    case 'image/png':
        $image = imagecreatefrompng($sourcePath);
        break;
    case 'image/gif':
        $image = imagecreatefromgif($sourcePath);
        break;
    default:
        die('Unsupported image formats,Supported only JPG、PNG and GIF。');
}

// Perform horizontal flip
if (imageflip($image, IMG_FLIP_HORIZONTAL)) {
    // Save new images
    imagejpeg($image, $destinationPath, 90);
    imagedestroy($image);
    echo 'The photo has been successfully flipped and saved as:<a href="https://m66.net/' . $destinationPath . '">View pictures</a>';
} else {
    die('Mirror flip failed,Please check the image resources。');
}
?>

Things to note

  1. Permissions issue : Make sure the uploads directory has write permissions, otherwise saving the image will fail.

  2. GD Library Support : Before using this feature, please make sure that PHP has installed and enabled the GD Image Library.

  3. Security : In actual development, uploaded files should be strictly verified to prevent security vulnerabilities.

summary

Using imageflip() can effectively achieve the mirror flip effect of selfies, which is a very practical trick when dealing with selfies. I hope this article will be helpful to your development in image processing!

If you are building a website with selfie upload function, don’t forget to add this function to let users see the “correct” themselves!