add image science resizing support, make it the default image processor

git-svn-id: http://svn.techno-weenie.net/projects/plugins/attachment_fu@2648 567b1171-46fb-0310-a4c9-b4bef9110e78
This commit is contained in:
technoweenie 2007-01-09 08:23:21 +00:00
Родитель d3551c6634
Коммит d35205c97b
6 изменённых файлов: 36 добавлений и 22 удалений

Просмотреть файл

@ -8,6 +8,7 @@ Tempfile.class_eval do
end
end
require 'geometry'
ActiveRecord::Base.send(:extend, Technoweenie::AttachmentFu::ActMethods)
Technoweenie::AttachmentFu.tempfile_path = ATTACHMENT_FU_TEMPFILE_PATH if Object.const_defined?(:ATTACHMENT_FU_TEMPFILE_PATH)
FileUtils.mkdir_p Technoweenie::AttachmentFu.tempfile_path

Просмотреть файл

@ -1,6 +1,6 @@
module Technoweenie # :nodoc:
module AttachmentFu # :nodoc:
@@default_processors = %w(Rmagick ImageScience)
@@default_processors = %w(ImageScience Rmagick)
@@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

Просмотреть файл

@ -19,17 +19,30 @@ module Technoweenie # :nodoc:
def process_attachment_with_processing
return unless process_attachment_without_processing || !image?
with_image { |img| resize_image_or_thumbnail! img }
with_image do |img|
self.width = img.width if respond_to?(:width)
self.height = img.height if respond_to?(:height)
callback_with_args :after_resize, img
end
end
# Performs the actual resizing operation for a thumbnail
def resize_image(img, size)
self.temp_path = write_to_temp_file('foo')
img.thumbnail(temp_path, (size.is_a?(Array) ? size.first : size).to_i)
# create a dummy temp file to write to
self.temp_path = write_to_temp_file(filename)
grab_dimensions = lambda do |img|
self.width = img.width if respond_to?(:width)
self.height = img.height if respond_to?(:height)
img.save temp_path
callback_with_args :after_resize, img
end
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)
img.thumbnail(size, &grab_dimensions)
else
img.resize(size[0], size[1], &grab_dimensions)
end
else
new_size = [img.width, img.height] / size.to_s
img.resize(new_size[0], new_size[1], &grab_dimensions)
end
end
end
end

9
test/fixtures/attachment.rb поставляемый
Просмотреть файл

@ -83,9 +83,10 @@ class MinimalAttachment < ActiveRecord::Base
end
end
class ImageScienceAttachment < ActiveRecord::Base
if Object.const_defined?(:ImageScience)
begin
class ImageScienceAttachment < ActiveRecord::Base
has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
:processor => :image_science, :thumbnails => { :thumb => [50, 50], :geometry => '53>', :width => 40 }, :resize_to => [55,55]
:processor => :image_science, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
end
end
rescue MissingSourceFile
end

Просмотреть файл

@ -1,5 +1,5 @@
require 'test/unit'
require File.expand_path(File.join(File.dirname(__FILE__), '../lib/geometry'))
require File.expand_path(File.join(File.dirname(__FILE__), '../lib/geometry')) unless Object.const_defined?(:Geometry)
class GeometryTest < Test::Unit::TestCase
def test_should_resize

Просмотреть файл

@ -8,21 +8,20 @@ class ImageScienceTest < Test::Unit::TestCase
attachment = upload_file :filename => '/files/rails.png'
assert_valid attachment
assert attachment.image?
# test image science 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/ }
width = attachment.thumbnails.detect { |t| t.filename =~ /_width/ }
assert_equal 39, thumb.width
assert_equal 50, thumb.height
assert_equal 41, geo.width
assert_equal 53, geo.height
assert_equal 31, width.width
assert_equal 40, width.height
# 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