What potential issues can arise when displaying images on a website using PHP?

One potential issue when displaying images on a website using PHP is the risk of exposing sensitive file paths or directories to users. To solve this issue, it's important to ensure that the image paths are not directly accessible by users and to properly sanitize user input to prevent directory traversal attacks.

// Example code snippet to prevent directory traversal attacks
$imagePath = 'images/' . basename($_GET['image']);

if (file_exists($imagePath)) {
    // Output image
    header('Content-Type: image/jpeg');
    readfile($imagePath);
} else {
    // Handle error
    echo 'Image not found';
}