In PHP, what considerations should be taken into account when displaying user-uploaded images on the frontend to prevent security vulnerabilities like local file inclusion?

When displaying user-uploaded images on the frontend, it is important to prevent security vulnerabilities like local file inclusion by ensuring that the image path is sanitized and restricted to a specific directory. This can be done by using a whitelist approach to only allow access to specific image directories and filenames.

// Example code snippet to prevent local file inclusion when displaying user-uploaded images
$allowed_directories = array('uploads/images/');
$image_path = $_GET['image'];

// Check if the image path is within the allowed directories
if (in_array(dirname($image_path), $allowed_directories)) {
    // Display the image
    echo '<img src="' . $image_path . '" alt="User Uploaded Image">';
} else {
    // Display a default image or error message
    echo '<img src="default.jpg" alt="Default Image">';
}