I would like to let WordPress calculate the aspect ratio & orientation when the media is uploaded instead of doing this in the templates. Then I can call images and ask for image.orientation which will give me ‘portrait’ or ‘landscape’ as a value.
Example, how it would look like after uploading an image:
Is this possible or do I have to do this in the frontend?
You can write some code for it. Use http://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata to get the width and the height, and then if $width/$height > 1 => landscape, otherwise its portrait. Store this value in the attachment using: http://codex.wordpress.org/Function_Reference/wp_update_attachment_metadata.
You can use an action for this, such as:
Adding the solution as well, following from your code attempt (I HAVENT TESTED THIS):