What best practices should be followed when using img elements in PHP to display images on a webpage?

When using img elements in PHP to display images on a webpage, it is important to properly sanitize user input to prevent security vulnerabilities such as cross-site scripting (XSS) attacks. One way to do this is by using the htmlentities() function to encode the image source URL before outputting it in the img element. This helps to ensure that any malicious code within the URL is rendered harmless.

<?php
$imageUrl = htmlentities($_GET['image_url']);
echo '<img src="' . $imageUrl . '" alt="Image">';
?>