What are the advantages of using PHP scripts to stream images instead of direct file access?

Streaming images through PHP scripts instead of direct file access can provide several advantages such as increased security by preventing direct access to image files, allowing for dynamic image processing before display, and enabling easier implementation of access control or restrictions.

<?php
// Example PHP script to stream images
$imagePath = 'path/to/image.jpg';

if (file_exists($imagePath)) {
    $imageInfo = getimagesize($imagePath);
    
    header('Content-Type: ' . $imageInfo['mime']);
    readfile($imagePath);
} else {
    echo 'Image not found.';
}
?>