imageflip() function in PHP

The imageflip() function is used to flip an image using a given mode. This function modifies the image resource directly and is commonly used for creating mirror effects or rotating images.

Syntax

bool imageflip(resource $image, int $mode)

Parameters

  • $image − An image resource created using functions like imagecreatetruecolor(), imagecreatefrompng(), etc.

  • $mode − The flip mode constant. Possible values are:

    • IMG_FLIP_HORIZONTAL − Flips the image horizontally (left to right)
    • IMG_FLIP_VERTICAL − Flips the image vertically (top to bottom)
    • IMG_FLIP_BOTH − Flips the image both horizontally and vertically

Return Value

Returns TRUE on success or FALSE on failure.

Example

The following example demonstrates how to flip an image horizontally ?

<?php
    $img_file = '/images/sample-image.png';
    header('Content-type: image/png');
    $img = imagecreatefrompng($img_file);
    
    // Flip the image horizontally
    imageflip($img, IMG_FLIP_HORIZONTAL);
    
    // Output the flipped image
    imagepng($img);
    imagedestroy($img);
?>

Different Flip Modes

Here's how each flip mode affects the image ?

<?php
    $img = imagecreatefrompng('/images/original.png');
    
    // Create copies for different flip operations
    $horizontal = imageclone($img);
    $vertical = imageclone($img);
    $both = imageclone($img);
    
    // Apply different flip modes
    imageflip($horizontal, IMG_FLIP_HORIZONTAL);
    imageflip($vertical, IMG_FLIP_VERTICAL);
    imageflip($both, IMG_FLIP_BOTH);
    
    // Save flipped images
    imagepng($horizontal, '/output/horizontal_flip.png');
    imagepng($vertical, '/output/vertical_flip.png');
    imagepng($both, '/output/both_flip.png');
    
    // Clean up memory
    imagedestroy($img);
    imagedestroy($horizontal);
    imagedestroy($vertical);
    imagedestroy($both);
?>

Conclusion

The imageflip() function provides an easy way to flip images in PHP using GD library. It modifies the original image resource and supports horizontal, vertical, and combined flipping modes for various image manipulation needs.

Updated on: 2026-03-15T08:03:42+05:30

354 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements