What potential pitfalls should be considered when trying to display images in PHP code?

One potential pitfall when displaying images in PHP code is not properly sanitizing user input, which can lead to security vulnerabilities such as Cross-Site Scripting (XSS) attacks. To prevent this, always validate and sanitize user input before using it to display images on a webpage. Additionally, ensure that the image file paths are correctly formatted to prevent any errors when loading the images.

// Sanitize user input before using it to display images
$user_input = filter_var($_GET['image'], FILTER_SANITIZE_STRING);

// Validate the image file path
$image_path = 'images/' . basename($user_input);

// Display the image on the webpage
echo '<img src="' . $image_path . '" alt="Image">';