3 module Technoweenie # :nodoc:
4 module AttachmentFu # :nodoc:
7 def self.included(base)
8 base.send :extend, ClassMethods
9 base.alias_method_chain :process_attachment, :processing
13 # Yields a block containing a GD2 Image for the given binary data.
14 def with_image(file, &block)
15 im = GD2::Image.import(file)
21 def process_attachment_with_processing
22 return unless process_attachment_without_processing && image?
24 resize_image_or_thumbnail! img
25 self.width = img.width
26 self.height = img.height
27 callback_with_args :after_resize, img
31 # Performs the actual resizing operation for a thumbnail
32 def resize_image(img, size)
33 size = size.first if size.is_a?(Array) && size.length == 1
34 if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
36 # Borrowed from image science's #thumbnail method and adapted
38 scale = size.to_f / (img.width > img.height ? img.width.to_f : img.height.to_f)
39 img.resize!((img.width * scale).round(1), (img.height * scale).round(1), false)
41 img.resize!(size.first, size.last, false)
44 w, h = [img.width, img.height] / size.to_s
45 img.resize!(w, h, false)
47 temp_paths.unshift random_tempfile_filename
48 self.size = img.export(self.temp_path)