There are two problems here:

1) You don't ensure that the thumbnail size has the same aspect ratio as the photo size we selected.  Since you have to do that, consider putting this thumbnail size selection code in the same function where we select the photo size.

2) You select the last thumbnail size that is listed that fills the screen.  If you knew that the sizes were always guaranteed to be ordered from largest to smallest that would work.  Or if you knew that the were smallest to largest, then you could change this code to use a for() loop instead of forEach, and break out of it once you found the first one that was big enough.

But I think that actually, you have to add code to ensure that you find the smallest big enough thumbnail size.