What are the potential pitfalls of using imagecopy() in PHP when resizing images?

When using imagecopy() in PHP to resize images, one potential pitfall is that the resized image may appear distorted or pixelated if the aspect ratio is not maintained. To solve this issue, it is recommended to use imagecopyresampled() instead, which allows for smoother resizing by interpolating pixels.

// 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 using imagecopyresampled()
imagecopyresampled($resized_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);

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

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