Permalink
Browse files

Fixed a discrepancy in the comments for image_science_processor.rb wh…

…ere it said it was RMagick instead of Image Science.

Added GD Processor and accompanying tests
  • Loading branch information...
1 parent e1705ef commit e29c9e4a2507f8b6d0c26767f71f291138e0579a Jason Seifer committed Mar 13, 2008
@@ -1,6 +1,6 @@
module Technoweenie # :nodoc:
module AttachmentFu # :nodoc:
- @@default_processors = %w(ImageScience Rmagick MiniMagick)
+ @@default_processors = %w(Gd2 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', 'image/jpg']
mattr_reader :content_types, :tempfile_path, :default_processors
@@ -0,0 +1,54 @@
+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
+ self.temp_path = random_tempfile_filename
+ self.size = img.export(self.temp_path)
+ end
+
+ end
+ end
+ end
+end
@@ -9,7 +9,7 @@ def self.included(base)
end
module ClassMethods
- # Yields a block containing an RMagick Image for the given binary data.
+ # Yields a block containing an Image Science image for the given binary data.
def with_image(file, &block)
::ImageScience.with_image file, &block
end
@@ -105,6 +105,17 @@ class MiniMagickAttachment < ActiveRecord::Base
end
begin
+ class GD2Attachment < ActiveRecord::Base
+ has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
+ :processor => :gd2, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
+ end
+rescue MissingSourceFile
+ puts $!.message
+ puts "no GD2"
+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
@@ -0,0 +1,31 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
+
+class GD2Test < Test::Unit::TestCase
+ attachment_model GD2Attachment
+
+ if Object.const_defined?(:GD2)
+ def test_should_resize_image
+ attachment = upload_file :filename => '/files/rails.png'
+ assert_valid attachment
+ assert attachment.image?
+ # test gd2 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 "GD2 not loaded, tests not running"
+ end
+ end
+end
View
@@ -23,6 +23,17 @@
t.column :aspect_ratio, :float
end
+ create_table :gd2_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 :image_science_attachments, :force => true do |t|
t.column :parent_id, :integer
t.column :thumbnail, :string
View
@@ -1,7 +1,7 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
ENV['RAILS_ENV'] = 'test'
-ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '../../../..'
+ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
require 'test/unit'
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))

0 comments on commit e29c9e4

Please sign in to comment.