How can the code provided for creating photo thumbnails in PHP be improved to handle different image dimensions and aspect ratios effectively?

To handle different image dimensions and aspect ratios effectively when creating photo thumbnails in PHP, we can modify the code to calculate the thumbnail dimensions based on the original image's aspect ratio. This way, the thumbnails will maintain the correct proportions regardless of the input image's size.

function createThumbnail($src, $dest, $desired_width) {
    list($src_width, $src_height) = getimagesize($src);
    $src_aspect_ratio = $src_width / $src_height;
    
    $desired_height = $desired_width / $src_aspect_ratio;
    
    $thumb = imagecreatetruecolor($desired_width, $desired_height);
    $source = imagecreatefromjpeg($src);
    
    imagecopyresampled($thumb, $source, 0, 0, 0, 0, $desired_width, $desired_height, $src_width, $src_height);
    
    imagejpeg($thumb, $dest);
}