How can PHP be used to resize and scale images on a server?
To resize and scale images on a server using PHP, you can utilize the GD library which provides functions for image manipulation. You can load an image, create a new image with the desired dimensions, and then copy and resize the original image onto the new image. Finally, you can save the resized image to a file on the server.
// Load the original image
$originalImage = imagecreatefromjpeg('original.jpg');
// Get the dimensions of the original image
$originalWidth = imagesx($originalImage);
$originalHeight = imagesy($originalImage);
// Set the desired width and height for the resized image
$newWidth = 200;
$newHeight = 150;
// Create a new image with the desired dimensions
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
// Copy and resize the original image onto the new image
imagecopyresampled($resizedImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
// Save the resized image to a file
imagejpeg($resizedImage, 'resized.jpg');
// Free up memory
imagedestroy($originalImage);
imagedestroy($resizedImage);