How can PHP developers efficiently handle requests to convert images from portrait to landscape orientation using PHP scripts?

To efficiently handle requests to convert images from portrait to landscape orientation using PHP scripts, developers can utilize the GD library in PHP to manipulate images. By checking the dimensions of the image and then rotating or resizing it accordingly, developers can easily convert portrait images to landscape orientation.

<?php
// Load the image
$image = imagecreatefromjpeg('portrait.jpg');

// Get the dimensions of the image
$width = imagesx($image);
$height = imagesy($image);

// Check if the image is in portrait orientation
if ($height > $width) {
    // Create a new blank image with landscape dimensions
    $newImage = imagecreatetruecolor($height, $width);

    // Rotate the image 90 degrees counterclockwise
    for ($x = 0; $x < $width; $x++) {
        for ($y = 0; $y < $height; $y++) {
            $color = imagecolorat($image, $x, $y);
            imagesetpixel($newImage, $height - $y - 1, $x, $color);
        }
    }

    // Save the new image
    imagejpeg($newImage, 'landscape.jpg');

    // Free up memory
    imagedestroy($newImage);
} else {
    // Image is already in landscape orientation
    // Save the image as it is
    imagejpeg($image, 'landscape.jpg');
}

// Free up memory
imagedestroy($image);
?>