Sha256: 51862a5b8188225fc866a396807e6ac369c5c5c230c0ca1cf4a991b9537879eb

Contents?: true

Size: 1.85 KB

Versions: 37

Compression:

Stored size: 1.85 KB

Contents

require 'rubygems'
require 'gd2'
module Technoweenie # :nodoc:
  module AttachmentFu # :nodoc:
    module Processors
      module Gd2Processor
        def self.included(base)
          base.send :extend, ClassMethods
          base.alias_method_chain :process_attachment, :processing
        end
        
        module ClassMethods
          # Yields a block containing a GD2 Image for the given binary data.
          def with_image(file, &block)
            im = GD2::Image.import(file)
            block.call(im)
          end
        end

        protected
          def process_attachment_with_processing
            return unless process_attachment_without_processing && image?
            with_image do |img|
              resize_image_or_thumbnail! img
              self.width  = img.width
              self.height = img.height
              callback_with_args :after_resize, img
            end
          end

          # Performs the actual resizing operation for a thumbnail
          def resize_image(img, size)
            size = size.first if size.is_a?(Array) && size.length == 1
            if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
              if size.is_a?(Fixnum)
                # Borrowed from image science's #thumbnail method and adapted 
                # for this.
                scale = size.to_f / (img.width > img.height ? img.width.to_f : img.height.to_f)
                img.resize!((img.width * scale).round(1), (img.height * scale).round(1), false)
              else
                img.resize!(size.first, size.last, false) 
              end
            else
              w, h = [img.width, img.height] / size.to_s
              img.resize!(w, h, false)
            end
            temp_paths.unshift random_tempfile_filename
            self.size = img.export(self.temp_path)
          end

      end
    end
  end
end

Version data entries

37 entries across 37 versions & 8 rubygems

Version Path
atd-attachment_fu-1.0.20080507 lib/technoweenie/attachment_fu/processors/gd2_processor.rb
atd-attachment_fu-1.0.20081202 lib/technoweenie/attachment_fu/processors/gd2_processor.rb
futuresinc-attachment_fu-1.0.4 lib/technoweenie/attachment_fu/processors/gd2_processor.rb
ioquatix-attachment_fu-1.0.1 lib/attachment_fu/processors/gd2_processor.rb
popel-attachment_fu-1.0.4 lib/technoweenie/attachment_fu/processors/gd2_processor.rb
ncri_attachment_fu-0.1.8 lib/technoweenie/attachment_fu/processors/gd2_processor.rb
forgeos_core-1.9.5.rc2 test/dummy/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
radiant-page_attachments-extension-1.0.2 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
radiant-page_attachments-extension-1.0.0 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.13 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.12 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.11 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.10 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.9 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.8 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.7 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.6 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.5 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.4 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb
refinerycms-0.9.5.3 vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/gd2_processor.rb