What are some common methods for resizing images in PHP?

Resizing images in PHP can be done using various methods such as the GD library or ImageMagick. One common approach is to use the GD library functions like imagecreatefromjpeg(), imagecopyresampled(), and imagejpeg() to resize images while maintaining aspect ratio.

// Example using GD library to resize an image
function resizeImage($sourcePath, $targetPath, $maxWidth, $maxHeight) {
    list($sourceWidth, $sourceHeight) = getimagesize($sourcePath);
    $ratio = $sourceWidth / $sourceHeight;

    if ($maxWidth / $maxHeight > $ratio) {
        $maxWidth = $maxHeight * $ratio;
    } else {
        $maxHeight = $maxWidth / $ratio;
    }

    $image = imagecreatetruecolor($maxWidth, $maxHeight);
    $sourceImage = imagecreatefromjpeg($sourcePath);
    imagecopyresampled($image, $sourceImage, 0, 0, 0, 0, $maxWidth, $maxHeight, $sourceWidth, $sourceHeight);
    imagejpeg($image, $targetPath);
}