How can the delay caused by loading a large JPG file in an HTML page be prevented when using the file() function in PHP?

The delay caused by loading a large JPG file in an HTML page can be prevented by resizing the image before displaying it. This can be done using the PHP image functions to create a smaller version of the image file. By reducing the file size, the loading time will be significantly faster, resulting in a smoother user experience.

<?php
// Path to the large JPG file
$large_image = 'path/to/large/image.jpg';

// Load the large image file
$large_img = imagecreatefromjpeg($large_image);

// Get the dimensions of the large image
$large_width = imagesx($large_img);
$large_height = imagesy($large_img);

// Calculate the new dimensions for the resized image
$new_width = 300; // Set the desired width for the resized image
$new_height = ($large_height / $large_width) * $new_width;

// Create a new image with the new dimensions
$new_img = imagecreatetruecolor($new_width, $new_height);

// Resize the large image to fit the new dimensions
imagecopyresampled($new_img, $large_img, 0, 0, 0, 0, $new_width, $new_height, $large_width, $large_height);

// Output the resized image to the browser
header('Content-Type: image/jpeg');
imagejpeg($new_img);

// Free up memory
imagedestroy($large_img);
imagedestroy($new_img);
?>