What are some common methods to prevent direct access to images in PHP websites?

Direct access to images in PHP websites can be prevented by using a .htaccess file to restrict access to the images directory, or by using PHP to check if the request is coming from a valid source before serving the image.

// Prevent direct access to images using PHP
if(!isset($_SERVER['HTTP_REFERER'])) {
    header('HTTP/1.0 403 Forbidden');
    exit;
}

// Serve the image if the request is coming from a valid source
$imagePath = 'path/to/image.jpg';
header('Content-Type: image/jpeg');
readfile($imagePath);