You don't want to globally reverse width & height. Keep in mind, that the width/height of the image are not changed if it is rotated. The rotation takes care of that. Rotating and changing width/height in code would countermeasure itself. (This at least applies for the visual aspect of this problem.)
See sketch. 1) original image 2) change itemHeight / itemHeight 3) rotate
https://bugzilla.mozilla.org/attachment.cgi?id=795659

However, when doing math again the above statement has to be slightly altered.  From a mathemtical perspective height and width indeed have switch placed and all calculations need to factor this in.