+ class ImageThumbnailCrop < MiniMagickAttachment
+ has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
+ :thumbnails => { :square => "50x50c", :vertical => "30x60c", :horizontal => "60x30c"}
+
+ # TODO this is a bad duplication, this method is in the MiniMagick Processor
+ def self.calculate_offset(image_width,image_height,image_aspect,thumb_width,thumb_height,thumb_aspect)
+ # only crop if image is not smaller in both dimensions
+
+ # special cases, image smaller in one dimension then thumbsize
+ if image_width < thumb_width
+ offset = (image_height / 2) - (thumb_height / 2)
+ command = "#{image_width}x#{thumb_height}+0+#{offset}"
+ elsif image_height < thumb_height
+ offset = (image_width / 2) - (thumb_width / 2)
+ command = "#{thumb_width}x#{image_height}+#{offset}+0"
+
+ # normal thumbnail generation
+ # calculate height and offset y, width is fixed
+ elsif (image_aspect <= thumb_aspect or image_width < thumb_width) and image_height > thumb_height
+ height = image_width / thumb_aspect
+ offset = (image_height / 2) - (height / 2)
+ command = "#{image_width}x#{height}+0+#{offset}"
+ # calculate width and offset x, height is fixed
+ else
+ width = image_height * thumb_aspect
+ offset = (image_width / 2) - (width / 2)
+ command = "#{width}x#{image_height}+#{offset}+0"
+ end
+ # crop image
+ command
+ end
+ end
+