Are there specific security considerations to keep in mind when outputting images in PHP?

When outputting images in PHP, it is important to ensure that the images are not directly accessible by users. One way to address this security concern is to store the images outside of the web root directory and use PHP to serve the images to users. This way, users cannot access the images directly through the URL.

<?php
// Path to the image file
$imagePath = '/path/to/image.jpg';

// Set the appropriate header for the image
header('Content-Type: image/jpeg');

// Output the image
readfile($imagePath);