2 module Technoweenie # :nodoc:
3 module AttachmentFu # :nodoc:
5 module RmagickProcessor
6 def self.included(base)
7 base.send :extend, ClassMethods
8 base.alias_method_chain :process_attachment, :processing
12 # Yields a block containing an RMagick Image for the given binary data.
13 def with_image(file, &block)
15 binary_data = file.is_a?(Magick::Image) ? file : Magick::Image.read(file).first unless !Object.const_defined?(:Magick)
17 # Log the failure to load the image. This should match ::Magick::ImageMagickError
18 # but that would cause acts_as_attachment to require rmagick.
19 logger.debug("Exception working with image: #{$!}")
22 block.call binary_data if block && binary_data
29 def process_attachment_with_processing
30 return unless process_attachment_without_processing
32 resize_image_or_thumbnail! img
33 self.width = img.columns if respond_to?(:width)
34 self.height = img.rows if respond_to?(:height)
35 callback_with_args :after_resize, img
39 # Performs the actual resizing operation for a thumbnail
40 def resize_image(img, size)
41 size = size.first if size.is_a?(Array) && size.length == 1 && !size.first.is_a?(Fixnum)
42 if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
43 size = [size, size] if size.is_a?(Fixnum)
45 elsif size.is_a?(String) && size =~ /^c.*$/ # Image cropping - example geometry string: c75x75
46 dimensions = size[1..size.size].split("x")
47 img.crop_resized!(dimensions[0].to_i, dimensions[1].to_i)
49 img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols<1 ? 1 : cols, rows<1 ? 1 : rows) }
51 img.strip! unless attachment_options[:keep_profile]
52 temp_paths.unshift write_to_temp_file(img.to_blob)