Current Location: Home> Latest Articles> PHP scripting for batch-flipping of image files

PHP scripting for batch-flipping of image files

M66 2025-05-31

When processing images, especially in batch image processing scenarios, there is often a need to flip (horizontal or vertical) images. Although PHP is a server-side language, it can easily implement this function through its built-in GD image processing library. This article will teach you how to write a PHP script that batch overturns all image files in a specified directory, and saves the flipped image to another directory.

1. Preparation

Make sure your server environment has the GD library enabled. You can check it in the following ways:

 <?php
phpinfo();
?>

After accessing this script in the browser, search for GD Support . If it is Enabled, then you can perform image processing operations.

2. Basic logic

We need to complete the following steps:

  1. Specifies a source directory containing images;

  2. traverse all image files in the directory;

  3. Load each image using the GD library;

  4. Flip the image (horizontal or vertical);

  5. Save the flipped image to the target directory.

3. Sample code

Here is a complete example script:

 <?php

$sourceDir = __DIR__ . '/images/originals';
$targetDir = __DIR__ . '/images/flipped';

// Create a target directory(If it does not exist)
if (!file_exists($targetDir)) {
    mkdir($targetDir, 0755, true);
}

// Supported image types
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];

// Scan the files in the source directory
$files = scandir($sourceDir);

foreach ($files as $file) {
    $filePath = $sourceDir . '/' . $file;

    // jump over . and ..
    if (in_array($file, ['.', '..'])) {
        continue;
    }

    // Get file extension
    $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));

    // Filter non-image files
    if (!in_array($extension, $allowedExtensions)) {
        continue;
    }

    // Loading the image
    switch ($extension) {
        case 'jpg':
        case 'jpeg':
            $image = imagecreatefromjpeg($filePath);
            break;
        case 'png':
            $image = imagecreatefrompng($filePath);
            break;
        case 'gif':
            $image = imagecreatefromgif($filePath);
            break;
        default:
            continue 2;
    }

    // Get image size
    $width = imagesx($image);
    $height = imagesy($image);

    // Create an empty canvas
    $flipped = imagecreatetruecolor($width, $height);

    // Flip the image horizontally
    for ($x = 0; $x < $width; $x++) {
        imagecopy($flipped, $image, $width - $x - 1, 0, $x, 0, 1, $height);
    }

    // Save new images
    $targetPath = $targetDir . '/' . $file;
    switch ($extension) {
        case 'jpg':
        case 'jpeg':
            imagejpeg($flipped, $targetPath);
            break;
        case 'png':
            imagepng($flipped, $targetPath);
            break;
        case 'gif':
            imagegif($flipped, $targetPath);
            break;
    }

    // Clean the memory
    imagedestroy($image);
    imagedestroy($flipped);

    echo "Processed images:$file\n";
}

echo "Batch flip is completed。Visit the flipped image to go to:http://m66.net/images/flipped/";

?>

4. Things to note

  • Ensure that the directory permissions are set correctly and that the PHP process has permission to access and write to the target directory;

  • If you want to flip vertically, just modify the flip logic and adjust the coordinates in imagecopy() accordingly;

  • For transparent PNG or GIF images, additional transparent processing logic may be required, otherwise a black background will appear.

V. Conclusion

Through the PHP scripts provided in this article, you can quickly implement batch image flip operations. Whether it is batch sorting of pictures, automated processing, or online picture tool development, such small functions can greatly improve efficiency. If you plan to integrate this script into the website, don't forget to add a simple user interface or file upload logic to make the functions more complete!