public
Description: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
Homepage: http://weblog.techno-weenie.net
Clone URL: git://github.com/technoweenie/attachment_fu.git
Click here to lend your support to: attachment_fu and make a donation at www.pledgie.com !
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
technoweenie (author)
Tue Jan 09 00:23:21 -0800 2007
commit  d35205c97b2ff49922ec2be824c0b6b9957b76ed
tree    c255fe17ff042e04e97ad4e83858404d9e2da869
parent  d3551c6634b3dfd91f7adadd31ea2ee7d473eece
...
8
9
10
 
11
12
13
14
...
8
9
10
11
12
13
14
15
0
@@ -8,6 +8,7 @@ Tempfile.class_eval do
0
   end
0
 end
0
 
0
+require 'geometry'
0
 ActiveRecord::Base.send(:extend, Technoweenie::AttachmentFu::ActMethods)
0
 Technoweenie::AttachmentFu.tempfile_path = ATTACHMENT_FU_TEMPFILE_PATH if Object.const_defined?(:ATTACHMENT_FU_TEMPFILE_PATH)
0
 FileUtils.mkdir_p Technoweenie::AttachmentFu.tempfile_path
0
\ No newline at end of file
...
1
2
3
 
4
5
6
...
1
2
 
3
4
5
6
0
@@ -1,6 +1,6 @@
0
 module Technoweenie # :nodoc:
0
   module AttachmentFu # :nodoc:
0
- @@default_processors = %w(Rmagick ImageScience)
0
+ @@default_processors = %w(ImageScience Rmagick)
0
     @@tempfile_path = File.join(RAILS_ROOT, 'tmp', 'attachment_fu')
0
     @@content_types = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png']
0
     mattr_reader :content_types, :tempfile_path, :default_processors
...
19
20
21
22
 
 
 
 
 
 
 
23
24
 
25
26
27
28
29
30
31
32
 
 
 
 
 
 
 
 
 
 
 
33
34
35
...
19
20
21
 
22
23
24
25
26
27
28
29
30
31
32
33
 
34
 
 
 
 
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -19,17 +19,30 @@ module Technoweenie # :nodoc:
0
           def process_attachment_with_processing
0
             return unless process_attachment_without_processing || !image?
0
             with_image { |img| resize_image_or_thumbnail! img }
0
- with_image do |img|
0
+ end
0
+
0
+ # Performs the actual resizing operation for a thumbnail
0
+ def resize_image(img, size)
0
+ # create a dummy temp file to write to
0
+ self.temp_path = write_to_temp_file(filename)
0
+ grab_dimensions = lambda do |img|
0
               self.width = img.width if respond_to?(:width)
0
               self.height = img.height if respond_to?(:height)
0
+ img.save temp_path
0
               callback_with_args :after_resize, img
0
             end
0
- end
0
 
0
- # Performs the actual resizing operation for a thumbnail
0
- def resize_image(img, size)
0
- self.temp_path = write_to_temp_file('foo')
0
- img.thumbnail(temp_path, (size.is_a?(Array) ? size.first : size).to_i)
0
+ size = size.first if size.is_a?(Array) && size.length == 1
0
+ if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
0
+ if size.is_a?(Fixnum)
0
+ img.thumbnail(size, &grab_dimensions)
0
+ else
0
+ img.resize(size[0], size[1], &grab_dimensions)
0
+ end
0
+ else
0
+ new_size = [img.width, img.height] / size.to_s
0
+ img.resize(new_size[0], new_size[1], &grab_dimensions)
0
+ end
0
           end
0
       end
0
     end
...
83
84
85
86
87
 
 
88
89
 
90
91
 
 
92
...
83
84
85
 
 
86
87
88
 
89
90
 
91
92
93
0
@@ -83,9 +83,10 @@ class MinimalAttachment < ActiveRecord::Base
0
   end
0
 end
0
 
0
-class ImageScienceAttachment < ActiveRecord::Base
0
- if Object.const_defined?(:ImageScience)
0
+begin
0
+ class ImageScienceAttachment < ActiveRecord::Base
0
     has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
0
- :processor => :image_science, :thumbnails => { :thumb => [50, 50], :geometry => '53>', :width => 40 }, :resize_to => [55,55]
0
+ :processor => :image_science, :thumbnails => { :thumb => [50, 51], :geometry => '31>' }, :resize_to => 55
0
   end
0
-end
0
+rescue MissingSourceFile
0
+end
0
\ No newline at end of file
...
1
2
 
3
4
5
...
1
 
2
3
4
5
0
@@ -1,5 +1,5 @@
0
 require 'test/unit'
0
-require File.expand_path(File.join(File.dirname(__FILE__), '../lib/geometry'))
0
+require File.expand_path(File.join(File.dirname(__FILE__), '../lib/geometry')) unless Object.const_defined?(:Geometry)
0
 
0
 class GeometryTest < Test::Unit::TestCase
0
   def test_should_resize
...
8
9
10
 
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
 
 
 
 
 
 
26
27
28
...
8
9
10
11
12
13
14
15
16
 
17
 
 
 
 
 
 
 
 
18
19
20
21
22
23
24
25
26
27
0
@@ -8,21 +8,20 @@ class ImageScienceTest < Test::Unit::TestCase
0
       attachment = upload_file :filename => '/files/rails.png'
0
       assert_valid attachment
0
       assert attachment.image?
0
+ # test image science thumbnail
0
       assert_equal 43, attachment.width
0
       assert_equal 55, attachment.height
0
       
0
       thumb = attachment.thumbnails.detect { |t| t.filename =~ /_thumb/ }
0
       geo = attachment.thumbnails.detect { |t| t.filename =~ /_geometry/ }
0
- width = attachment.thumbnails.detect { |t| t.filename =~ /_width/ }
0
       
0
- assert_equal 39, thumb.width
0
- assert_equal 50, thumb.height
0
-
0
- assert_equal 41, geo.width
0
- assert_equal 53, geo.height
0
-
0
- assert_equal 31, width.width
0
- assert_equal 40, width.height
0
+ # test exact resize dimensions
0
+ assert_equal 50, thumb.width
0
+ assert_equal 51, thumb.height
0
+
0
+ # test geometry string
0
+ assert_equal 31, geo.width
0
+ assert_equal 40, geo.height
0
     end
0
   else
0
     def test_flunk

Comments

    No one has commented yet.