Current Location: Home> Latest Articles> How to achieve image flip based on user input in PHP?

How to achieve image flip based on user input in PHP?

gitbox 2025-05-19

In PHP, image processing is a common task that usually involves loading, modifying, and saving images. To enhance the user experience, we can programmatically implement some dynamic image effects, such as flipping the image according to the user's input. PHP's GD library provides the imageflip() function, which allows you to flip images easily.

This article will show you how to use the imageflip() function and dynamically flip the image according to the parameters entered by the user.

Step 1: Make sure your PHP environment supports GD library

Before using the imageflip() function, you need to make sure that your PHP environment supports the GD library. You can check by executing the following command:

 php -m | grep gd

If the command outputs gd , it means that your environment has installed the GD library. If not installed, you can install it in a Linux system through the following command:

 sudo apt-get install php-gd

After the installation is complete, restart the web server for the changes to take effect.

Step 2: Create an HTML form to receive user input

In order for the user to choose the type of image flip, we need to create a simple HTML form. In this form, the user can upload the image and select the flip method. The form code is as follows:

 <!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image flip</title>
</head>
<body>
    <h1>Upload the image and select the flip effect</h1>
    <form action="flip_image.php" method="POST" enctype="multipart/form-data">
        <label for="image">Select an image:</label>
        <input type="file" name="image" id="image" required><br><br>

        <label for="flip_type">Choose the flip method:</label>
        <select name="flip_type" id="flip_type">
            <option value="0">Vertical flip</option>
            <option value="1">Horizontal flip</option>
            <option value="2">垂直与Horizontal flip</option>
        </select><br><br>

        <input type="submit" value="submit">
    </form>
</body>
</html>

The form contains image upload functionality and provides a drop-down menu that allows the user to select the flip type (flip vertically, horizontally, or both).

Step 3: Process user uploads and apply image flip

Next, we create a PHP script ( flip_image.php ) that processes the images uploaded by the user and performs corresponding flip operations according to the selected flip method.

 <?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get uploaded files
    if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
        $imagePath = $_FILES['image']['tmp_name'];
        $flipType = isset($_POST['flip_type']) ? (int)$_POST['flip_type'] : 0;

        // Loading the image
        $image = imagecreatefromjpeg($imagePath);
        if (!$image) {
            die('无法Loading the image');
        }

        // Apply flip
        $flipResult = imageflip($image, $flipType);

        // Check if the flip is successful
        if ($flipResult) {
            header('Content-Type: image/jpeg');
            imagejpeg($image);
            imagedestroy($image);
        } else {
            echo 'Image flip失败';
        }
    } else {
        echo 'Please select a valid image file';
    }
}
?>

Code explanation:

  1. Uploading images : First, we check whether the user has uploaded a valid image file.

  2. Loading image : Use the imagecreatefromjpeg() function to load the user uploaded JPEG image. For images in other formats such as PNG or GIF, you can use imagecreatefrommpng() or imagecreatefromgif() .

  3. Flip the image : Call imageflip() function to flip according to the flip type selected by the user. The parameters of the imageflip() function can be:

    • 0 : Vertical flip

    • 1 : Horizontal flip

    • 2 : Vertical and horizontal flip

  4. Output image : After successfully flipping the image, we use imagejpeg() to output the result image and destroy the image resources.

Step 4: Save or display the flipped image

If you want to save the flipped image to the server instead of displaying it directly, you can use the file path parameter of the imagejpeg() function. For example:

 imagejpeg($image, 'path/to/save/flipped_image.jpg');

In this way, the flipped image will be saved at the specified location of the server.

Summarize

Through PHP's imageflip() function, we can easily flip the image, and users can choose vertical, horizontal, or both as needed. Just a small amount of code is required to add image processing to your web application.