What are the potential pitfalls of using imagecreate functions in PHP for resizing images?

One potential pitfall of using imagecreate functions in PHP for resizing images is that it can lead to loss of image quality or distortion if not done properly. To avoid this, it is recommended to use imagecopyresampled function instead, as it provides better quality resizing by interpolating the image data.

// 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;
$new_image = imagecreatetruecolor($new_width, $new_height);

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

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

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