What are best practices for ensuring that dynamically generated images in PHP are always up to date and not cached?

When dynamically generating images in PHP, it's important to prevent caching to ensure that the images are always up to date. One way to achieve this is by adding a unique query string parameter to the image URL, which changes whenever the image content is updated. This forces the browser to fetch the image from the server each time, rather than using a cached version.

<?php
// Generate a unique query string parameter based on the current timestamp
$timestamp = time();
$imageUrl = "generate_image.php?timestamp=$timestamp";

// Output the image tag with the dynamic URL
echo "<img src='$imageUrl' alt='Dynamic Image'>";
?>