1 require 'red_artisan/core_image/processor'
3 module Technoweenie # :nodoc:
4 module AttachmentFu # :nodoc:
6 module CoreImageProcessor
7 def self.included(base)
8 base.send :extend, ClassMethods
9 base.alias_method_chain :process_attachment, :processing
13 def with_image(file, &block)
14 block.call OSX::CIImage.from(file)
19 def process_attachment_with_processing
20 return unless process_attachment_without_processing
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
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))
37 processor.resize(size[0], size[1])
40 new_size = [img.extent.size.width, img.extent.size.height] / size.to_s
41 processor.resize(new_size[0], new_size[1])
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)
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)