What are some best practices for efficiently managing and displaying images in a PHP-based photo gallery?

Efficiently managing and displaying images in a PHP-based photo gallery involves optimizing image sizes, implementing lazy loading, and utilizing caching techniques to improve performance.

// Example code snippet for efficiently managing and displaying images in a PHP-based photo gallery

// Function to resize images to a specific width and height
function resize_image($file, $w, $h) {
    list($width, $height) = getimagesize($file);
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($w, $h);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
    return $dst;
}

// Example usage:
$resized_image = resize_image('image.jpg', 300, 200);
imagejpeg($resized_image, 'resized_image.jpg', 100);