Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add MiniMagick support to attachment_fu [Isacc]

git-svn-id: http://svn.techno-weenie.net/projects/plugins/attachment_fu@2744 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information...
commit adf7694cdbe014c032847551d27be9294d5e94bd 1 parent 0d479b7
technoweenie authored
View
2  lib/technoweenie/attachment_fu.rb
@@ -1,6 +1,6 @@
module Technoweenie # :nodoc:
module AttachmentFu # :nodoc:
- @@default_processors = %w(ImageScience Rmagick)
+ @@default_processors = %w(ImageScience Rmagick MiniMagick)
@@tempfile_path = File.join(RAILS_ROOT, 'tmp', 'attachment_fu')
@@content_types = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png']
mattr_reader :content_types, :tempfile_path, :default_processors
View
56 lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb
@@ -0,0 +1,56 @@
+require 'mini_magick'
+module Technoweenie # :nodoc:
+ module AttachmentFu # :nodoc:
+ module Processors
+ module MiniMagickProcessor
+ def self.included(base)
+ base.send :extend, ClassMethods
+ base.alias_method_chain :process_attachment, :processing
+ end
+
+ module ClassMethods
+ # Yields a block containing an MiniMagick Image for the given binary data.
+ def with_image(file, &block)
+ begin
+ binary_data = file.is_a?(MiniMagick::Image) ? file : MiniMagick::Image.from_file(file) unless !Object.const_defined?(:MiniMagick)
+ rescue
+ # Log the failure to load the image.
+ logger.debug("Exception working with image: #{$!}")
+ binary_data = nil
+ end
+ block.call binary_data if block && binary_data
+ ensure
+ !binary_data.nil?
+ end
+ end
+
+ protected
+ def process_attachment_with_processing
+ return unless process_attachment_without_processing
+ with_image do |img|
+ resize_image_or_thumbnail! img
+ self.width = img[:width] if respond_to?(:width)
+ self.height = img[:height] if respond_to?(:height)
+ callback_with_args :after_resize, img
+ end if image?
+ 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)
+ size = [size, size]
+ img.resize(size.join('x'))
+ else
+ img.resize(size.join('x') + '!')
+ end
+ else
+ img.resize(size.to_s)
+ end
+ self.temp_path = img.path
+ end
+ end
+ end
+ end
+end
View
42 lib/technoweenie/attachment_fu/processors/rmagick_processor.rb
@@ -25,28 +25,28 @@ def with_image(file, &block)
end
end
- protected
- def process_attachment_with_processing
- return unless process_attachment_without_processing
- with_image do |img|
- resize_image_or_thumbnail! img
- self.width = img.columns if respond_to?(:width)
- self.height = img.rows if respond_to?(:height)
- callback_with_args :after_resize, img
- end if image?
- 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 && !size.first.is_a?(Fixnum)
- if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
- size = [size, size] if size.is_a?(Fixnum)
- img.thumbnail!(*size)
- else
- img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols, rows) }
- end
- self.temp_path = write_to_temp_file(img.to_blob)
+ protected
+ def process_attachment_with_processing
+ return unless process_attachment_without_processing
+ with_image do |img|
+ resize_image_or_thumbnail! img
+ self.width = img.columns if respond_to?(:width)
+ self.height = img.rows if respond_to?(:height)
+ callback_with_args :after_resize, img
+ end if image?
+ 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 && !size.first.is_a?(Fixnum)
+ if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
+ size = [size, size] if size.is_a?(Fixnum)
+ img.thumbnail!(*size)
+ else
+ img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols, rows) }
end
+ self.temp_path = write_to_temp_file(img.to_blob)
+ end
end
end
end
View
20 test/fixtures/attachment.rb
@@ -90,6 +90,26 @@ class ImageScienceAttachment < ActiveRecord::Base
:processor => :image_science, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
end
rescue MissingSourceFile
+ puts $!.message
+ puts "no ImageScience"
+end
+
+begin
+ class MiniMagickAttachment < ActiveRecord::Base
+ has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
+ :processor => :mini_magick, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
+ end
+rescue MissingSourceFile
+ puts $!.message
+ puts "no Mini Magick"
+end
+
+begin
+ class MiniMagickAttachment < ActiveRecord::Base
+ has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
+ :processor => :mini_magick, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
+ end
+rescue MissingSourceFile
end
begin
View
31 test/processors/mini_magick_test.rb
@@ -0,0 +1,31 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+class MiniMagickTest < Test::Unit::TestCase
+ attachment_model MiniMagickAttachment
+
+ if Object.const_defined?(:MiniMagick)
+ def test_should_resize_image
+ attachment = upload_file :filename => '/files/rails.png'
+ assert_valid attachment
+ assert attachment.image?
+ # test MiniMagick thumbnail
+ assert_equal 43, attachment.width
+ assert_equal 55, attachment.height
+
+ thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
+ geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
+
+ # test exact resize dimensions
+ assert_equal 50, thumb.width
+ assert_equal 51, thumb.height
+
+ # test geometry string
+ assert_equal 31, geo.width
+ assert_equal 40, geo.height
+ end
+ else
+ def test_flunk
+ puts "MiniMagick not loaded, tests not running"
+ end
+ end
+end
View
22 test/schema.rb
@@ -34,6 +34,28 @@
t.column :type, :string
end
+ create_table :mini_magick_attachments, :force => true do |t|
+ t.column :parent_id, :integer
+ t.column :thumbnail, :string
+ t.column :filename, :string, :limit => 255
+ t.column :content_type, :string, :limit => 255
+ t.column :size, :integer
+ t.column :width, :integer
+ t.column :height, :integer
+ t.column :type, :string
+ end
+
+ create_table :mini_magick_attachments, :force => true do |t|
+ t.column :parent_id, :integer
+ t.column :thumbnail, :string
+ t.column :filename, :string, :limit => 255
+ t.column :content_type, :string, :limit => 255
+ t.column :size, :integer
+ t.column :width, :integer
+ t.column :height, :integer
+ t.column :type, :string
+ end
+
create_table :orphan_attachments, :force => true do |t|
t.column :db_file_id, :integer
t.column :filename, :string, :limit => 255
Please sign in to comment.
Something went wrong with that request. Please try again.