merged in from code from the other master
[selectricity] / vendor / plugins / attachment_fu / lib / technoweenie / attachment_fu / processors / gd2_processor.rb
1 require 'rubygems'
2 require 'gd2'
3 module Technoweenie # :nodoc:
4   module AttachmentFu # :nodoc:
5     module Processors
6       module Gd2Processor
7         def self.included(base)
8           base.send :extend, ClassMethods
9           base.alias_method_chain :process_attachment, :processing
10         end
11         
12         module ClassMethods
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)
16             block.call(im)
17           end
18         end
19
20         protected
21           def process_attachment_with_processing
22             return unless process_attachment_without_processing && image?
23             with_image do |img|
24               resize_image_or_thumbnail! img
25               self.width  = img.width
26               self.height = img.height
27               callback_with_args :after_resize, img
28             end
29           end
30
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))
35               if size.is_a?(Fixnum)
36                 # Borrowed from image science's #thumbnail method and adapted 
37                 # for this.
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)
40               else
41                 img.resize!(size.first, size.last, false) 
42               end
43             else
44               w, h = [img.width, img.height] / size.to_s
45               img.resize!(w, h, false)
46             end
47             temp_paths.unshift random_tempfile_filename
48             self.size = img.export(self.temp_path)
49           end
50
51       end
52     end
53   end
54 end

Benjamin Mako Hill || Want to submit a patch?