How can PHP be used to resize images and add a white border to create consistent thumbnails?
To resize images and add a white border to create consistent thumbnails using PHP, you can use the GD library functions. First, you need to load the image, resize it to the desired dimensions, create a new image with a white border, and then copy the resized image onto the new image with a border.
// Load the original image
$original_image = imagecreatefromjpeg('original.jpg');
// Get the original image dimensions
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);
// Define the thumbnail dimensions
$thumbnail_width = 100;
$thumbnail_height = 100;
// Create a new image with white border
$thumbnail_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
$white = imagecolorallocate($thumbnail_image, 255, 255, 255);
imagefilledrectangle($thumbnail_image, 0, 0, $thumbnail_width, $thumbnail_height, $white);
// Resize and copy the original image onto the thumbnail image
imagecopyresampled($thumbnail_image, $original_image, 5, 5, 0, 0, $thumbnail_width - 10, $thumbnail_height - 10, $original_width, $original_height);
// Output the thumbnail image
header('Content-Type: image/jpeg');
imagejpeg($thumbnail_image);
// Free up memory
imagedestroy($original_image);
imagedestroy($thumbnail_image);