What potential issues can arise when displaying images using PHP?

One potential issue that can arise when displaying images using PHP is that the images may not be properly resized or scaled, leading to distortion or poor quality. To solve this, you can use the PHP GD library to resize the images before displaying them.

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

// Get the original dimensions
$width = imagesx($image);
$height = imagesy($image);

// Set the new dimensions
$newWidth = 200;
$newHeight = ($height / $width) * $newWidth;

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

// Resize the original image to the new dimensions
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

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

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