You can use add_image_size to determine sizes that require one dimension, and scale the other proportionally:
add_image_size('width-640', 640, 0, false); add_image_size('height-640', 0, 640, false);
You can also achieve this with a single line
add_image_size('constrain-640', 640, 640, false);
This cuts the image proportionally so that its longest side does not exceed 640 pixels.
source share