What are some common pitfalls when resizing images in PHP for display in a div element?

Common pitfalls when resizing images in PHP for display in a div element include distorting the aspect ratio of the image, losing image quality, and potential performance issues. To solve these issues, it is important to use PHP's image processing functions like `imagecreatefromjpeg`, `imagecopyresized`, and `imagejpeg` to resize the image while maintaining its aspect ratio and quality.

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

// Get the original image dimensions
$originalWidth = imagesx($originalImage);
$originalHeight = imagesy($originalImage);

// Calculate the new dimensions for the resized image
$newWidth = 300; // desired width
$newHeight = floor($originalHeight * ($newWidth / $originalWidth));

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

// Resize the original image to the new dimensions
imagecopyresized($resizedImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);

// Output the resized image to the browser
header('Content-Type: image/jpeg');
imagejpeg($resizedImage);

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