What is the purpose of the createThumb function in the provided PHP code?
The purpose of the createThumb function in the provided PHP code is to generate a thumbnail image from a given image file. This function resizes the original image to a specified width and height, while maintaining the aspect ratio of the original image. It then saves the thumbnail image to a specified location on the server.
function createThumb($source, $destination, $width, $height) {
$source_img = imagecreatefromjpeg($source);
$thumb_img = imagecreatetruecolor($width, $height);
$source_width = imagesx($source_img);
$source_height = imagesy($source_img);
$thumb_width = $width;
$thumb_height = $height;
$source_aspect_ratio = $source_width / $source_height;
$thumb_aspect_ratio = $thumb_width / $thumb_height;
if ($source_aspect_ratio > $thumb_aspect_ratio) {
$new_height = $thumb_height;
$new_width = $source_width / ($source_height / $thumb_height);
} else {
$new_width = $thumb_width;
$new_height = $source_height / ($source_width / $thumb_width);
}
imagecopyresampled($thumb_img, $source_img, 0, 0, 0, 0, $new_width, $new_height, $source_width, $source_height);
imagejpeg($thumb_img, $destination);
imagedestroy($source_img);
imagedestroy($thumb_img);
}
// Example usage
createThumb('original.jpg', 'thumbnail.jpg', 100, 100);
Related questions
-
PHP date formatting time formatting
-
PHP development tools pre-packaged solution WT-NMP
-
PHP configuration settings dedicated class