What is the purpose of displaying thumbnails without saving them in PHP?

Displaying thumbnails without saving them in PHP can help improve performance by reducing the amount of disk space used and the number of file operations performed. This can be achieved by generating the thumbnails on the fly and serving them directly to the user without saving them to the server. This approach is particularly useful when dealing with a large number of images or when the thumbnails are only needed temporarily.

<?php
// Path to the original image
$original_image = 'path/to/original/image.jpg';

// Create a thumbnail on-the-fly
$thumbnail = imagecreatefromjpeg($original_image);
$thumb_width = 100;
$thumb_height = 100;
$width = imagesx($thumbnail);
$height = imagesy($thumbnail);
$new_width = $thumb_width;
$new_height = floor($height * ($thumb_width / $width));
$new_thumbnail = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($new_thumbnail, $thumbnail, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

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

// Clean up
imagedestroy($thumbnail);
imagedestroy($new_thumbnail);
?>