Is there a preferred function to use for resizing images in PHP, such as imagecopyresample?

When resizing images in PHP, the preferred function to use is imagecopyresampled(). This function allows you to resize images while maintaining the aspect ratio and image quality. It is a more advanced version of imagecopyresample() and produces better results when resizing images.

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

// Get the dimensions of the original image
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);

// Create a new image with the desired dimensions
$new_width = 200;
$new_height = 150;
$resized_image = imagecreatetruecolor($new_width, $new_height);

// Resize the original image to the new dimensions
imagecopyresampled($resized_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

// Save the resized image
imagejpeg($resized_image, 'resized.jpg');

// Free up memory
imagedestroy($original_image);
imagedestroy($resized_image);