Backend Development 5 min read

Rotating and Flipping Images with PHP GD: Using imagerotate and Custom Functions

This article explains the difference between image rotation and flipping, introduces PHP's GD imagerotate() function, and provides complete PHP code examples for rotating JPEG images as well as flipping them horizontally or vertically using custom functions.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Rotating and Flipping Images with PHP GD: Using imagerotate and Custom Functions

Image rotation and flipping are common tasks in web projects, but they are distinct concepts: rotation turns an image by a specific angle, while flipping mirrors the image along a chosen axis. Rotation can be performed directly with the GD library's imagerotate() function, whose prototype is:

resource imagerotate(resource src_im, float angle, int bgd_color [, int ignore_transparent])

The function rotates src_im by angle degrees, fills uncovered areas with bgd_color , and rotates around the image centre; the result is scaled to fit the original dimensions. Setting ignore_transparent to a non‑zero value discards transparent pixels.

Below is a PHP example that defines a rotate() function to rotate a JPEG image by a given number of degrees and saves the result back to the original file:

<?php
// Rotate an image by a given angle (JPEG example)
function rotate($filename, $degrees) {
    // Create image resource
    $source = imagecreatefromjpeg($filename);
    // Rotate using imagerotate()
    $rotate = imagerotate($source, $degrees, 0);
    // Save the rotated image
    imagejpeg($rotate, $filename);
}
// Rotate brophp.jpg by 180 degrees
rotate("brophp.jpg", 180);
?>

Flipping an image cannot be done with an arbitrary angle; it is limited to two directions: horizontal flip along the Y‑axis or vertical flip along the X‑axis.

For a horizontal (Y‑axis) flip, the following turn_y() function copies each column of pixels from right to left into a new image resource and saves the result:

<?php
function turn_y($filename) {
    $back = imagecreatefromjpeg($filename);
    $width = imagesx($back);
    $height = imagesy($back);
    $new = imagecreatetruecolor($width, $height);
    for ($x = 0; $x < $width; $x++) {
        imagecopy($new, $back, $width - $x - 1, 0, $x, 0, 1, $height);
    }
    imagejpeg($new, $filename);
    imagedestroy($back);
    imagedestroy($new);
}
turn_y("brophp.jpg");
?>

For a vertical (X‑axis) flip, the turn_x() function mirrors the image top‑to‑bottom by copying each row of pixels from bottom to top:

<?php
function turn_x($filename) {
    $back = imagecreatefromjpeg($filename);
    $width = imagesx($back);
    $height = imagesy($back);
    $new = imagecreatetruecolor($width, $height);
    for ($y = 0; $y < $height; $y++) {
        imagecopy($new, $back, 0, $height - $y - 1, 0, $y, $width, 1);
    }
    imagejpeg($new, $filename);
    imagedestroy($back);
    imagedestroy($new);
}
turn_x("brophp.jpg");
?>

These examples demonstrate how to implement both rotation and axis‑specific flipping of JPEG images using PHP's GD extension.

backendPHPGDImage Flippingimage rotation
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.