What are the best practices for optimizing image display and download options on a website using PHP?

To optimize image display and download options on a website using PHP, it is important to resize images to appropriate dimensions for web display, lazy load images to improve page loading speed, and provide download options for users to access high-resolution images if needed.

// Example code for resizing images using PHP GD library
function resize_image($file, $new_width, $new_height) {
    list($width, $height) = getimagesize($file);
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($file);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    return $image_p;
}

// Example code for lazy loading images using PHP
echo '<img src="placeholder.jpg" data-src="image.jpg" class="lazyload" />';

// Example code for providing download options for images
echo '<a href="image.jpg" download>Download High-Resolution Image</a>';