+ img.combine_options do |commands|
+ commands.strip unless attachment_options[:keep_profile]
+
+ # gif are not handled correct, this is a hack, but it seems to work.
+ if img.output =~ / GIF /
+ img.format("png")
+ end
+
+ if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
+ if size.is_a?(Fixnum)
+ size = [size, size]
+ commands.resize(size.join('x'))
+ else
+ commands.resize(size.join('x') + '!')
+ end
+ # extend to thumbnail size
+ elsif size.is_a?(String) and size =~ /e$/
+ size = size.gsub(/e/, '')
+ commands.resize(size.to_s + '>')
+ commands.background('#ffffff')
+ commands.gravity('center')
+ commands.extent(size)
+ # crop thumbnail, the smart way
+ elsif size.is_a?(String) and size =~ /c$/
+ size = size.gsub(/c/, '')
+
+ # calculate sizes and aspect ratio
+ thumb_width, thumb_height = size.split("x")
+ thumb_width = thumb_width.to_f
+ thumb_height = thumb_height.to_f
+
+ thumb_aspect = thumb_width.to_f / thumb_height.to_f
+ image_width, image_height = img[:width].to_f, img[:height].to_f
+ image_aspect = image_width / image_height
+
+ # only crop if image is not smaller in both dimensions
+ unless image_width < thumb_width and image_height < thumb_height
+ command = calculate_offset(image_width,image_height,image_aspect,thumb_width,thumb_height,thumb_aspect)
+
+ # crop image
+ commands.extract(command)
+ end
+
+ # don not resize if image is not as height or width then thumbnail
+ if image_width < thumb_width or image_height < thumb_height
+ commands.background('#ffffff')
+ commands.gravity('center')
+ commands.extent(size)
+ # resize image
+ else
+ commands.resize("#{size.to_s}")
+ end
+ # crop end