merged in from code from the other master
[selectricity-live] / vendor / plugins / attachment_fu / lib / technoweenie / attachment_fu / processors / core_image_processor.rb
1 require 'red_artisan/core_image/processor'
2
3 module Technoweenie # :nodoc:
4   module AttachmentFu # :nodoc:
5     module Processors
6       module CoreImageProcessor
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           def with_image(file, &block)
14             block.call OSX::CIImage.from(file)
15           end
16         end
17                 
18         protected
19           def process_attachment_with_processing
20             return unless process_attachment_without_processing
21             with_image do |img|
22               self.width  = img.extent.size.width  if respond_to?(:width)
23               self.height = img.extent.size.height if respond_to?(:height)
24               resize_image_or_thumbnail! img
25               callback_with_args :after_resize, img
26             end if image?
27           end
28
29           # Performs the actual resizing operation for a thumbnail
30           def resize_image(img, size)
31             processor = ::RedArtisan::CoreImage::Processor.new(img)
32             size = size.first if size.is_a?(Array) && size.length == 1
33             if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
34               if size.is_a?(Fixnum)
35                 processor.fit(size)
36               else
37                 processor.resize(size[0], size[1])
38               end
39             else
40               new_size = [img.extent.size.width, img.extent.size.height] / size.to_s
41               processor.resize(new_size[0], new_size[1])
42             end
43             
44             processor.render do |result|
45               self.width  = result.extent.size.width  if respond_to?(:width)
46               self.height = result.extent.size.height if respond_to?(:height)
47               
48               # Get a new temp_path for the image before saving
49               temp_paths.unshift Tempfile.new(random_tempfile_filename, Technoweenie::AttachmentFu.tempfile_path).path
50               result.save self.temp_path, OSX::NSJPEGFileType
51               self.size = File.size(self.temp_path)
52             end
53           end          
54       end
55     end
56   end
57 end
58
59

Benjamin Mako Hill || Want to submit a patch?