Topic: Paperclip dynamic styles based on resize algorithm

I'm trying to get Paperclip to resize my uploaded images to sizes determined by an algorithm which resizes images to a specified area (w*h) to the multiple of n, whether the image is landscape or portrait orientation. It's for the user interface.

Examples:

W391 × H215 (medium original) resizes to W180 × H98 (thumbnail)
W738 × H471 (large original) resizes to W180 × H114 (thumbnail)

I'm new to Paperclip and Rails actually. I already have my algorithm. I want to know where should my resizing algorithm go and how. Your help is much appreciated.