Skip to content
PHP Logo

How does resizing an image to thumbnail size affect the quality and appearance of the thumbnail in PHP?

Resizing an image to thumbnail size in PHP can affect the quality and appearance of the thumbnail if not done properly. To maintain the quality and appearance, it is important to use image manipulation functions like imagecopyresampled() or imagecopyresized() to resize the image while preserving its aspect ratio.

php
// Load the original image
$original_image = imagecreatefromjpeg('original.jpg');

// Get the dimensions of the original image
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);

// Set the desired thumbnail width and height
$thumbnail_width = 100;
$thumbnail_height = 100;

// Create a new image for the thumbnail
$thumbnail_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);

// Resize the original image to thumbnail size
imagecopyresampled($thumbnail_image, $original_image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $original_width, $original_height);

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

// Clean up
imagedestroy($original_image);
imagedestroy($thumbnail_image);