How does PHP handle different image formats like PNG, BMP, and GIF when generating thumbnails?

When generating thumbnails in PHP, you can use the GD library to handle different image formats like PNG, BMP, and GIF. GD library provides functions to create thumbnails from various image formats and allows you to specify the output format for the thumbnail.

// Example code to generate a thumbnail from an image in PHP using GD library
$sourceImage = 'image.png'; // Path to the source image
$thumbnailWidth = 100; // Width of the thumbnail
$thumbnailHeight = 100; // Height of the thumbnail

// Create a new image resource from the source image
$source = imagecreatefrompng($sourceImage);

// Create a new true color image for the thumbnail
$thumbnail = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);

// Generate the thumbnail from the source image
imagecopyresampled($thumbnail, $source, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, imagesx($source), imagesy($source));

// Output the thumbnail to the browser or save it to a file
header('Content-Type: image/png');
imagepng($thumbnail);