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 !
refactor AttachmentFu::Pixels
rick (author)
Wed Apr 23 15:54:19 -0700 2008
commit  7f9fe1cbb8aecd2fb9ef370d3706c543fee597a0
tree    208171c1064a4c1ad8821692f4384bed0c32af91
parent  3792dbd2105580d8536214ca92f7ebcf74f15062
...
1
2
 
3
4
5
...
8
9
10
11
12
13
14
15
16
17
18
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
...
1
 
2
3
4
5
...
8
9
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
 
12
13
14
15
 
 
 
16
17
18
19
0
@@ -1,5 +1,5 @@
0
 module AttachmentFu
0
- class Pixels
0
+ module Pixels
0
     def self.[](key)
0
       @@key_to_class ||= {}
0
       @@key_to_class[key] ||= begin
0
@@ -8,38 +8,12 @@ module AttachmentFu
0
         const_get(path.classify)
0
       end
0
     end
0
-
0
- attr_accessor :file
0
-
0
- def initialize(processor, file = nil, &block)
0
- @file = file
0
- extend self.class[processor]
0
- instance_eval(&block) if block
0
- end
0
-
0
- def self.resize_task
0
- lambda do |attachment, options|
0
- # this is going to change
0
- # PDI a simple configurable order
0
- # task :resize, :with => [:core_image, :gd, :image_science, :rmagick]
0
- #
0
- options[:with] ||= :mojo_magick
0
- new options[:with], attachment.full_filename do
0
- data = with_image { |img| resize_image img, :size => options[:to], :to => options[:destination] }
0
- unless options[:skip_size]
0
- attachment.width = data.width if attachment.respond_to?(:width)
0
- attachment.height = data.height if attachment.respond_to?(:height)
0
- end
0
- end
0
- end
0
- end
0
 
0
- def self.image_size_task
0
- lambda do |attachment, options|
0
+ # Base class for all Pixel-related tasks
0
+ class Task
0
+ def initialize(klass, options)
0
         options[:with] ||= :mojo_magick
0
- new options[:with], attachment.full_filename do
0
- attachment.width, attachment.height = with_image { |img| get_image_size(img) }
0
- end
0
+ extend AttachmentFu::Pixels[options[:with]]
0
       end
0
     end
0
 
...
2
3
4
5
 
6
7
8
 
 
9
10
11
...
2
3
4
 
5
6
 
 
7
8
9
10
11
0
@@ -2,10 +2,10 @@ require 'red_artisan/core_image/processor'
0
 require 'attachment_fu/geometry'
0
 
0
 module AttachmentFu # :nodoc:
0
- class Pixels
0
+ module Pixels
0
     module CoreImage
0
- def with_image(&block)
0
- block.call OSX::CIImage.from(@file)
0
+ def with_image(attachment, &block)
0
+ block.call OSX::CIImage.from(attachment.full_filename)
0
       end
0
 
0
       def get_image_size(image)
...
3
4
5
6
 
7
8
9
 
 
10
11
12
...
3
4
5
 
6
7
 
 
8
9
10
11
12
0
@@ -3,10 +3,10 @@ require 'mojo_magick/mojo_magick'
0
 require 'attachment_fu/geometry'
0
 
0
 module AttachmentFu # :nodoc:
0
- class Pixels
0
+ module Pixels
0
     module MojoMagick
0
- def with_image(&block)
0
- block.call @file
0
+ def with_image(attachment, &block)
0
+ block.call attachment.full_filename
0
       end
0
 
0
       def get_image_size(image)
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
6
7
 
 
8
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
 
25
26
27
28
0
@@ -1,6 +1,26 @@
0
 require 'attachment_fu/pixels'
0
 
0
+module AttachmentFu
0
+ class Tasks
0
+ class Resize < AttachmentFu::Pixels::Task
0
+ def call(attachment, options)
0
+ data = with_image(attachment) { |img| resize_image img, :size => options[:to], :to => options[:destination] }
0
+ unless options[:skip_size]
0
+ attachment.width = data.width if attachment.respond_to?(:width)
0
+ attachment.height = data.height if attachment.respond_to?(:height)
0
+ end
0
+ end
0
+ end
0
+
0
+ class ImageSize < AttachmentFu::Pixels::Task
0
+ def call(attachment, options)
0
+ attachment.width, attachment.height = with_image(attachment) { |img| get_image_size(img) }
0
+ end
0
+ end
0
+ end
0
+end
0
+
0
 # task :resize, :with => :mojo_magic, :to => '50x50'
0
 #
0
-AttachmentFu.create_task :resize, AttachmentFu::Pixels.resize_task
0
-AttachmentFu.create_task :get_image_size, AttachmentFu::Pixels.image_size_task
0
\ No newline at end of file
0
+AttachmentFu.create_task :resize, AttachmentFu::Tasks::Resize
0
+AttachmentFu.create_task :get_image_size, AttachmentFu::Tasks::ImageSize
0
\ No newline at end of file
...
1
2
 
3
4
5
...
77
78
79
80
81
 
82
...
1
 
2
3
4
5
...
77
78
79
 
80
81
82
0
@@ -1,5 +1,5 @@
0
 module AttachmentFu
0
- class Pixels
0
+ class Tasks
0
     class Thumbnails
0
       # Some valid options:
0
       #
0
@@ -77,4 +77,4 @@ module AttachmentFu
0
   end
0
 end
0
 
0
-AttachmentFu.create_task :thumbnails, AttachmentFu::Pixels::Thumbnails
0
\ No newline at end of file
0
+AttachmentFu.create_task :thumbnails, AttachmentFu::Tasks::Thumbnails
0
\ No newline at end of file
...
4
5
6
7
 
8
9
10
...
13
14
15
16
 
 
17
18
19
20
 
21
22
23
24
25
26
27
 
28
29
30
...
33
34
35
36
 
37
38
39
...
42
43
44
45
 
46
47
48
...
4
5
6
 
7
8
9
10
...
13
14
15
 
16
17
18
19
20
 
21
22
23
24
25
26
27
 
28
29
30
31
...
34
35
36
 
37
38
39
40
...
43
44
45
 
46
47
48
49
0
@@ -4,7 +4,7 @@ describe AttachmentFu::Pixels::CoreImage do
0
   before :all do
0
     FileUtils.mkdir_p AttachmentFu.root_path
0
     @samples = File.join(File.dirname(__FILE__), 'samples')
0
- @pixels = AttachmentFu::Pixels.new :core_image
0
+ @pixels = AttachmentFu::Tasks::Resize.new Class, :with => :core_image
0
   end
0
     
0
   after :all do
0
@@ -13,18 +13,19 @@ describe AttachmentFu::Pixels::CoreImage do
0
   
0
   describe "(for JPG)" do
0
     before do
0
- @pixels.file = File.join(@samples, 'casshern.jpg')
0
+ @attachment = mock("Attachment")
0
+ @attachment.stub!(:full_filename).and_return(File.join(@samples, 'casshern.jpg'))
0
     end
0
 
0
     it "gets accurate dimensions" do
0
- @pixels.with_image do |image|
0
+ @pixels.with_image(@attachment) do |image|
0
         image.extent.size.width.should == 80
0
         image.extent.size.height.should == 75
0
       end
0
     end
0
     
0
     it "resizes image with geometry string" do
0
- @pixels.with_image do |image|
0
+ @pixels.with_image(@attachment) do |image|
0
         data = @pixels.resize_image image, :size => '40x40', :to => File.join(AttachmentFu.root_path, 'resized.jpg')
0
         data.width.should == 40
0
         data.height.should == 38
0
@@ -33,7 +34,7 @@ describe AttachmentFu::Pixels::CoreImage do
0
     end
0
     
0
     it "resizes image with integer" do
0
- @pixels.with_image do |image|
0
+ @pixels.with_image(@attachment) do |image|
0
         data = @pixels.resize_image image, :size => 40, :to => File.join(AttachmentFu.root_path, 'resized.jpg')
0
         data.width.should == 40
0
         data.height.should == 37
0
@@ -42,7 +43,7 @@ describe AttachmentFu::Pixels::CoreImage do
0
     end
0
     
0
     it "resizes image with array" do
0
- @pixels.with_image do |image|
0
+ @pixels.with_image(@attachment) do |image|
0
         data = @pixels.resize_image image, :size => [40, 40], :to => File.join(AttachmentFu.root_path, 'resized.jpg')
0
         data.width.should == 40
0
         data.height.should == 40
...
4
5
6
7
 
8
9
10
...
13
14
15
16
 
 
17
18
19
20
 
21
22
23
24
25
26
 
27
28
29
...
32
33
34
35
 
36
37
38
...
41
42
43
44
 
45
46
47
...
4
5
6
 
7
8
9
10
...
13
14
15
 
16
17
18
19
20
 
21
22
23
24
25
26
 
27
28
29
30
...
33
34
35
 
36
37
38
39
...
42
43
44
 
45
46
47
48
0
@@ -4,7 +4,7 @@ describe AttachmentFu::Pixels::MojoMagick do
0
   before :all do
0
     FileUtils.mkdir_p AttachmentFu.root_path
0
     @samples = File.join(File.dirname(__FILE__), 'samples')
0
- @pixels = AttachmentFu::Pixels.new :mojo_magick
0
+ @pixels = AttachmentFu::Tasks::Resize.new Class, :with => :mojo_magick
0
   end
0
     
0
   after :all do
0
@@ -13,17 +13,18 @@ describe AttachmentFu::Pixels::MojoMagick do
0
   
0
   describe "(for JPG)" do
0
     before do
0
- @pixels.file = File.join(@samples, 'casshern.jpg')
0
+ @attachment = mock("Attachment")
0
+ @attachment.stub!(:full_filename).and_return(File.join(@samples, 'casshern.jpg'))
0
     end
0
 
0
     it "gets accurate dimensions" do
0
- @pixels.with_image do |image|
0
+ @pixels.with_image(@attachment) do |image|
0
         ::MojoMagick.get_image_size(image).should == {:width => 80, :height => 75}
0
       end
0
     end
0
     
0
     it "resizes image with geometry string" do
0
- @pixels.with_image do |image|
0
+ @pixels.with_image(@attachment) do |image|
0
         data = @pixels.resize_image image, :size => '40x40', :to => File.join(AttachmentFu.root_path, 'resized.jpg')
0
         data.width.should == 40
0
         data.height.should == 38
0
@@ -32,7 +33,7 @@ describe AttachmentFu::Pixels::MojoMagick do
0
     end
0
     
0
     it "resizes image with integer" do
0
- @pixels.with_image do |image|
0
+ @pixels.with_image(@attachment) do |image|
0
         data = @pixels.resize_image image, :size => 40, :to => File.join(AttachmentFu.root_path, 'resized.jpg')
0
         data.width.should == 40
0
         data.height.should == 38
0
@@ -41,7 +42,7 @@ describe AttachmentFu::Pixels::MojoMagick do
0
     end
0
     
0
     it "resizes image with array" do
0
- @pixels.with_image do |image|
0
+ @pixels.with_image(@attachment) do |image|
0
         data = @pixels.resize_image image, :size => [40, 40], :to => File.join(AttachmentFu.root_path, 'resized.jpg')
0
         data.width.should == 40
0
         data.height.should == 38
...
24
25
26
27
28
29
30
...
24
25
26
 
27
28
29
0
@@ -24,7 +24,6 @@ module AttachmentFu
0
     end
0
     
0
     it "resizes image" do
0
- @pixels = AttachmentFu::Pixels.new :mojo_magick, @asset.full_filename
0
       @asset.width.should == 40
0
       @asset.height.should == 38
0
     end

Comments

    No one has commented yet.