1 require 'image_science'
2 module Technoweenie # :nodoc:
3 module AttachmentFu # :nodoc:
5 module ImageScienceProcessor
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)
14 ::ImageScience.with_image file, &block
19 def process_attachment_with_processing
20 return unless process_attachment_without_processing && image?
22 self.width = img.width if respond_to?(:width)
23 self.height = img.height if respond_to?(:height)
24 resize_image_or_thumbnail! img
28 # Performs the actual resizing operation for a thumbnail
29 def resize_image(img, size)
30 # create a dummy temp file to write to
31 # ImageScience doesn't handle all gifs properly, so it converts them to
32 # pngs for thumbnails. It has something to do with trying to save gifs
33 # with a larger palette than 256 colors, which is all the gif format
35 filename.sub! /gif$/, 'png'
36 content_type.sub!(/gif$/, 'png')
37 self.temp_path = write_to_temp_file(filename)
38 grab_dimensions = lambda do |img|
39 self.width = img.width if respond_to?(:width)
40 self.height = img.height if respond_to?(:height)
41 img.save self.temp_path
42 self.size = File.size(self.temp_path)
43 callback_with_args :after_resize, img
46 size = size.first if size.is_a?(Array) && size.length == 1
47 if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
49 img.thumbnail(size, &grab_dimensions)
51 img.resize(size[0], size[1], &grab_dimensions)
54 new_size = [img.width, img.height] / size.to_s
55 img.resize(new_size[0], new_size[1], &grab_dimensions)