/* Check which dimension has the smallest distance, making it suitable for a straight resize. The other dimension can then be padded to restore the ratio. */ if(($src_width - $dest_width) >= ($src_height - $dest_height)) { $dest_height = ($dest_width/$src_width) * $src_height; } else { $dest_width = ($dest_height/$src_height) * $src_width; }