How can PHP functions like getimagesize() and imagecopyresampled() be used to resize images effectively?

To resize images effectively using PHP functions like getimagesize() and imagecopyresampled(), you can first determine the dimensions of the original image using getimagesize(). Then, create a new image with the desired dimensions using imagecreatetruecolor(). Finally, use imagecopyresampled() to copy and resize the original image onto the new image.

// Get the dimensions of the original image
list($width, $height) = getimagesize('original_image.jpg');

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

// Create a new image with the desired dimensions
$newImage = imagecreatetruecolor($newWidth, $newHeight);

// Load the original image
$originalImage = imagecreatefromjpeg('original_image.jpg');

// Resize the original image and copy it to the new image
imagecopyresampled($newImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

// Save the resized image to a new file
imagejpeg($newImage, 'resized_image.jpg');

// Free up memory
imagedestroy($originalImage);
imagedestroy($newImage);