This is already implemented in FrankerFaceZ, search the settings for “Portrait Mode” and choose “Auto”.