How can the createThumb function be optimized for better performance when generating thumbnails?
The createThumb function can be optimized for better performance by utilizing caching mechanisms to store generated thumbnails and avoid regenerating them unnecessarily. By checking if a thumbnail already exists before generating it, unnecessary processing can be avoided, resulting in faster thumbnail generation times.
function createThumb($imagePath, $thumbPath, $thumbWidth, $thumbHeight) {
if (!file_exists($thumbPath)) {
$image = imagecreatefromjpeg($imagePath);
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($image), imagesy($image));
imagejpeg($thumb, $thumbPath);
imagedestroy($image);
imagedestroy($thumb);
}
}