Current Location: Home> Latest Articles> Automatically flip the image when uploading

Automatically flip the image when uploading

M66 2025-05-17

In many cases, uploaded images may need to be flipped on the server side due to shooting angle problems. Here is how to automatically flip uploaded images when using PHP. This article will explain how to use PHP and GD libraries to process images and automatically flip them when uploading them.

1. Introduce the GD library

PHP comes with a GD library, which can be used for image processing. First, make sure your PHP environment has GD library enabled. You can check if the GD library is enabled by running the following code:

 <?php
if (extension_loaded('gd')) {
    echo "GDThe library is enabled!";
} else {
    echo "GDLibrary not enabled!";
}
?>

2. Upload the picture and flip it

To demonstrate how to flip when uploading images, we will write a simple image upload form and processing logic. Here is an upload form and corresponding processing PHP code:

HTML upload form

 <!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image upload</title>
</head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <label for="file">Select a picture:</label>
        <input type="file" name="file" id="file" required>
        <button type="submit">Upload</button>
    </form>
</body>
</html>

Image upload and flip processing

 <?php
// 配置Upload的目标目录
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);

// Check if the file is an image
if (isset($_POST["submit"])) {
    $check = getimagesize($_FILES["file"]["tmp_name"]);
    if ($check !== false) {
        echo "The file is a picture - " . $check["mime"] . ".";
    } else {
        echo "File is not a picture。";
        exit;
    }
}

// 移动Upload的document到目标目录
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
    echo "document " . htmlspecialchars(basename($_FILES["file"]["name"])) . " 已成功Upload。";
} else {
    echo "Feel sorry,Uploaddocument时出错。";
}

// Image flip processing
function flipImage($filePath) {
    // Get the size and type of image
    $image_info = getimagesize($filePath);
    $image_type = $image_info[2];

    // Create image resources based on image type
    switch ($image_type) {
        case IMAGETYPE_JPEG:
            $image = imagecreatefromjpeg($filePath);
            break;
        case IMAGETYPE_PNG:
            $image = imagecreatefrompng($filePath);
            break;
        case IMAGETYPE_GIF:
            $image = imagecreatefromgif($filePath);
            break;
        default:
            echo "不支持的document类型!";
            return;
    }

    // Flip the image
    $flipped_image = imagerotate($image, 180, 0); // 180degree rotation,Image flip

    // Save the flipped image
    switch ($image_type) {
        case IMAGETYPE_JPEG:
            imagejpeg($flipped_image, $filePath);
            break;
        case IMAGETYPE_PNG:
            imagepng($flipped_image, $filePath);
            break;
        case IMAGETYPE_GIF:
            imagegif($flipped_image, $filePath);
            break;
    }

    // Release image resources
    imagedestroy($image);
    imagedestroy($flipped_image);

    echo "The picture has been successfully flipped!";
}

// Calling the flip function
flipImage($target_file);
?>

3. Code parsing

  • Upload form : The user submits the form through the browser and uploads the image file.

  • Image processing : When a user uploads an image, we check the file type through the getimagesize() function and confirm that it is an image file. Then we use move_uploaded_file() to save the file to the specified directory.

  • FlipImage() function is responsible for flipping the uploaded image . By using the imagerotate() function, we rotate the image 180 degrees to achieve the flip effect. The flipped image will overwrite the original file.

4. Summary

Through the above steps, we have successfully implemented a function of uploading and automatically flipping images. Regardless of whether the user uploads pictures in JPEG, PNG or GIF format, the system can correctly identify and flip.

Through this method, we can ensure that the uploaded images always conform to the correct direction and improve the user experience.