What are some recommended functions or methods in PHP for manipulating and resizing images?

When working with images in PHP, it is common to need functions or methods for manipulating and resizing images. Some recommended functions for this purpose include imagecreatefromjpeg(), imagecreatefrompng(), imagecopyresampled(), and imagejpeg(). These functions allow you to create image resources from JPEG or PNG files, resize images while maintaining quality, and save the manipulated images as JPEG files.

// Example of resizing and saving an image in PHP
$originalImage = 'original.jpg';
$resizedImage = 'resized.jpg';

// Create a new image resource from the original image
$source = imagecreatefromjpeg($originalImage);

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

// Set the desired width and height for the resized image
$newWidth = 200;
$newHeight = 150;

// Create a new image resource for the resized image
$destination = imagecreatetruecolor($newWidth, $newHeight);

// Resize the original image to fit the new dimensions
imagecopyresampled($destination, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

// Save the resized image as a JPEG file
imagejpeg($destination, $resizedImage);

// Free up memory by destroying the image resources
imagedestroy($source);
imagedestroy($destination);