artmotion-attachment_fu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/CHANGELOG +35 -0
  2. data/README +186 -0
  3. data/Rakefile +22 -0
  4. data/amazon_s3.yml.tpl +14 -0
  5. data/init.rb +16 -0
  6. data/install.rb +5 -0
  7. data/lib/gem_init.rb +2 -0
  8. data/lib/geometry.rb +93 -0
  9. data/lib/technoweenie/attachment_fu/backends/db_file_backend.rb +39 -0
  10. data/lib/technoweenie/attachment_fu/backends/file_system_backend.rb +101 -0
  11. data/lib/technoweenie/attachment_fu/backends/s3_backend.rb +303 -0
  12. data/lib/technoweenie/attachment_fu/processors/core_image_processor.rb +59 -0
  13. data/lib/technoweenie/attachment_fu/processors/gd2_processor.rb +54 -0
  14. data/lib/technoweenie/attachment_fu/processors/image_science_processor.rb +61 -0
  15. data/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb +59 -0
  16. data/lib/technoweenie/attachment_fu/processors/rmagick_processor.rb +54 -0
  17. data/lib/technoweenie/attachment_fu.rb +473 -0
  18. data/test/backends/db_file_test.rb +16 -0
  19. data/test/backends/file_system_test.rb +80 -0
  20. data/test/backends/remote/s3_test.rb +107 -0
  21. data/test/base_attachment_tests.rb +77 -0
  22. data/test/basic_test.rb +70 -0
  23. data/test/database.yml +18 -0
  24. data/test/extra_attachment_test.rb +67 -0
  25. data/test/fixtures/attachment.rb +148 -0
  26. data/test/fixtures/files/fake/rails.png +0 -0
  27. data/test/fixtures/files/foo.txt +1 -0
  28. data/test/fixtures/files/rails.png +0 -0
  29. data/test/geometry_test.rb +101 -0
  30. data/test/processors/core_image_test.rb +37 -0
  31. data/test/processors/gd2_test.rb +31 -0
  32. data/test/processors/image_science_test.rb +31 -0
  33. data/test/processors/mini_magick_test.rb +31 -0
  34. data/test/processors/rmagick_test.rb +255 -0
  35. data/test/schema.rb +108 -0
  36. data/test/test_helper.rb +150 -0
  37. data/test/validation_test.rb +55 -0
  38. data/vendor/red_artisan/core_image/filters/color.rb +27 -0
  39. data/vendor/red_artisan/core_image/filters/effects.rb +31 -0
  40. data/vendor/red_artisan/core_image/filters/perspective.rb +25 -0
  41. data/vendor/red_artisan/core_image/filters/quality.rb +25 -0
  42. data/vendor/red_artisan/core_image/filters/scale.rb +47 -0
  43. data/vendor/red_artisan/core_image/filters/watermark.rb +32 -0
  44. data/vendor/red_artisan/core_image/processor.rb +123 -0
  45. metadata +109 -0
@@ -0,0 +1,47 @@
1
+ module RedArtisan
2
+ module CoreImage
3
+ module Filters
4
+ module Scale
5
+
6
+ def resize(width, height)
7
+ create_core_image_context(width, height)
8
+
9
+ scale_x, scale_y = scale(width, height)
10
+
11
+ @original.affine_clamp :inputTransform => OSX::NSAffineTransform.transform do |clamped|
12
+ clamped.lanczos_scale_transform :inputScale => scale_x > scale_y ? scale_x : scale_y, :inputAspectRatio => scale_x / scale_y do |scaled|
13
+ scaled.crop :inputRectangle => vector(0, 0, width, height) do |cropped|
14
+ @target = cropped
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ def thumbnail(width, height)
21
+ create_core_image_context(width, height)
22
+
23
+ transform = OSX::NSAffineTransform.transform
24
+ transform.scaleXBy_yBy *scale(width, height)
25
+
26
+ @original.affine_transform :inputTransform => transform do |scaled|
27
+ @target = scaled
28
+ end
29
+ end
30
+
31
+ def fit(size)
32
+ original_size = @original.extent.size
33
+ scale = size.to_f / (original_size.width > original_size.height ? original_size.width : original_size.height)
34
+ resize (original_size.width * scale).to_i, (original_size.height * scale).to_i
35
+ end
36
+
37
+ private
38
+
39
+ def scale(width, height)
40
+ original_size = @original.extent.size
41
+ return width.to_f / original_size.width.to_f, height.to_f / original_size.height.to_f
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,32 @@
1
+ module RedArtisan
2
+ module CoreImage
3
+ module Filters
4
+ module Watermark
5
+
6
+ def watermark(watermark_image, tile = false, strength = 0.1)
7
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
8
+
9
+ if watermark_image.respond_to? :to_str
10
+ watermark_image = OSX::CIImage.from(watermark_image.to_str)
11
+ end
12
+
13
+ if tile
14
+ tile_transform = OSX::NSAffineTransform.transform
15
+ tile_transform.scaleXBy_yBy 1.0, 1.0
16
+
17
+ watermark_image.affine_tile :inputTransform => tile_transform do |tiled|
18
+ tiled.crop :inputRectangle => vector(0, 0, @original.extent.size.width, @original.extent.size.height) do |tiled_watermark|
19
+ watermark_image = tiled_watermark
20
+ end
21
+ end
22
+ end
23
+
24
+ @original.dissolve_transition :inputTargetImage => watermark_image, :inputTime => strength do |watermarked|
25
+ @target = watermarked
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,123 @@
1
+ require 'rubygems'
2
+ require 'osx/cocoa'
3
+ require 'active_support'
4
+
5
+ require 'red_artisan/core_image/filters/scale'
6
+ require 'red_artisan/core_image/filters/color'
7
+ require 'red_artisan/core_image/filters/watermark'
8
+ require 'red_artisan/core_image/filters/quality'
9
+ require 'red_artisan/core_image/filters/perspective'
10
+ require 'red_artisan/core_image/filters/effects'
11
+
12
+ # Generic image processor for scaling images based on CoreImage via RubyCocoa.
13
+ #
14
+ # Example usage:
15
+ #
16
+ # p = Processor.new OSX::CIImage.from(path_to_image)
17
+ # p.resize(640, 480)
18
+ # p.render do |result|
19
+ # result.save('resized.jpg', OSX::NSJPEGFileType)
20
+ # end
21
+ #
22
+ # This will resize the image to the given dimensions exactly, if you'd like to ensure that aspect ratio is preserved:
23
+ #
24
+ # p = Processor.new OSX::CIImage.from(path_to_image)
25
+ # p.fit(640)
26
+ # p.render do |result|
27
+ # result.save('resized.jpg', OSX::NSJPEGFileType)
28
+ # end
29
+ #
30
+ # fit(size) will attempt its best to resize the image so that the longest width/height (depending on image orientation) will match
31
+ # the given size. The second axis will be calculated automatically based on the aspect ratio.
32
+ #
33
+ # Scaling is performed by first clamping the image so that its external bounds become infinite, this helps when scaling so that any
34
+ # rounding discrepencies in dimensions don't affect the resultant image. We then perform a Lanczos transform on the image which scales
35
+ # it to the target size. We then crop the image to the traget dimensions.
36
+ #
37
+ # If you are generating smaller images such as thumbnails where high quality rendering isn't as important, an additional method is
38
+ # available:
39
+ #
40
+ # p = Processor.new OSX::CIImage.from(path_to_image)
41
+ # p.thumbnail(100, 100)
42
+ # p.render do |result|
43
+ # result.save('resized.jpg', OSX::NSJPEGFileType)
44
+ # end
45
+ #
46
+ # This will perform a straight affine transform and scale the X and Y boundaries to the requested size. Generally, this will be faster
47
+ # than a lanczos scale transform, but with a scaling quality trade.
48
+ #
49
+ # More than welcome to intregrate any patches, improvements - feel free to mail me with ideas.
50
+ #
51
+ # Thanks to
52
+ # * Satoshi Nakagawa for working out that OCObjWrapper needs inclusion when aliasing method_missing on existing OSX::* classes.
53
+ # * Vasantha Crabb for general help and inspiration with Cocoa
54
+ # * Ben Schwarz for example image data and collaboration during performance testing
55
+ #
56
+ # Copyright (c) Marcus Crafter <crafterm@redartisan.com> released under the MIT license
57
+ #
58
+ module RedArtisan
59
+ module CoreImage
60
+ class Processor
61
+
62
+ def initialize(original)
63
+ if original.respond_to? :to_str
64
+ @original = OSX::CIImage.from(original.to_str)
65
+ else
66
+ @original = original
67
+ end
68
+ end
69
+
70
+ def render(&block)
71
+ raise "unprocessed image: #{@original}" unless @target
72
+ block.call @target
73
+ end
74
+
75
+ include Filters::Scale, Filters::Color, Filters::Watermark, Filters::Quality, Filters::Perspective, Filters::Effects
76
+
77
+ private
78
+
79
+ def create_core_image_context(width, height)
80
+ output = OSX::NSBitmapImageRep.alloc.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel(nil, width, height, 8, 4, true, false, OSX::NSDeviceRGBColorSpace, 0, 0)
81
+ context = OSX::NSGraphicsContext.graphicsContextWithBitmapImageRep(output)
82
+ OSX::NSGraphicsContext.setCurrentContext(context)
83
+ @ci_context = context.CIContext
84
+ end
85
+
86
+ def vector(x, y, w, h)
87
+ OSX::CIVector.vectorWithX_Y_Z_W(x, y, w, h)
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ module OSX
94
+ class CIImage
95
+ include OCObjWrapper
96
+
97
+ def method_missing_with_filter_processing(sym, *args, &block)
98
+ f = OSX::CIFilter.filterWithName("CI#{sym.to_s.camelize}")
99
+ return method_missing_without_filter_processing(sym, *args, &block) unless f
100
+
101
+ f.setDefaults if f.respond_to? :setDefaults
102
+ f.setValue_forKey(self, 'inputImage')
103
+ options = args.last.is_a?(Hash) ? args.last : {}
104
+ options.each { |k, v| f.setValue_forKey(v, k.to_s) }
105
+
106
+ block.call f.valueForKey('outputImage')
107
+ end
108
+
109
+ alias_method_chain :method_missing, :filter_processing
110
+
111
+ def save(target, format = OSX::NSJPEGFileType, properties = nil)
112
+ bitmapRep = OSX::NSBitmapImageRep.alloc.initWithCIImage(self)
113
+ blob = bitmapRep.representationUsingType_properties(format, properties)
114
+ blob.writeToFile_atomically(target, false)
115
+ end
116
+
117
+ def self.from(filepath)
118
+ raise Errno::ENOENT, "No such file or directory - #{filepath}" unless File.exists?(filepath)
119
+ OSX::CIImage.imageWithContentsOfURL(OSX::NSURL.fileURLWithPath(filepath))
120
+ end
121
+ end
122
+ end
123
+
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artmotion-attachment_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rick Olson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-25 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: attachment_fu is a plugin by Rick Olson (aka technoweenie <http://techno-weenie.net>) and is the successor to acts_as_attachment.
17
+ email: technoweenie@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - CHANGELOG
26
+ - README
27
+ - Rakefile
28
+ - amazon_s3.yml.tpl
29
+ - init.rb
30
+ - install.rb
31
+ - lib/gem_init.rb
32
+ - lib/geometry.rb
33
+ - lib/technoweenie
34
+ - lib/technoweenie/attachment_fu.rb
35
+ - lib/technoweenie/attachment_fu
36
+ - lib/technoweenie/attachment_fu/processors
37
+ - lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb
38
+ - lib/technoweenie/attachment_fu/processors/gd2_processor.rb
39
+ - lib/technoweenie/attachment_fu/processors/core_image_processor.rb
40
+ - lib/technoweenie/attachment_fu/processors/image_science_processor.rb
41
+ - lib/technoweenie/attachment_fu/processors/rmagick_processor.rb
42
+ - lib/technoweenie/attachment_fu/backends
43
+ - lib/technoweenie/attachment_fu/backends/db_file_backend.rb
44
+ - lib/technoweenie/attachment_fu/backends/file_system_backend.rb
45
+ - lib/technoweenie/attachment_fu/backends/s3_backend.rb
46
+ - vendor/red_artisan
47
+ - vendor/red_artisan/core_image
48
+ - vendor/red_artisan/core_image/processor.rb
49
+ - vendor/red_artisan/core_image/filters
50
+ - vendor/red_artisan/core_image/filters/scale.rb
51
+ - vendor/red_artisan/core_image/filters/color.rb
52
+ - vendor/red_artisan/core_image/filters/perspective.rb
53
+ - vendor/red_artisan/core_image/filters/quality.rb
54
+ - vendor/red_artisan/core_image/filters/watermark.rb
55
+ - vendor/red_artisan/core_image/filters/effects.rb
56
+ - test/basic_test.rb
57
+ - test/schema.rb
58
+ - test/base_attachment_tests.rb
59
+ - test/fixtures
60
+ - test/fixtures/files
61
+ - test/fixtures/files/fake
62
+ - test/fixtures/files/fake/rails.png
63
+ - test/fixtures/files/foo.txt
64
+ - test/fixtures/files/rails.png
65
+ - test/fixtures/attachment.rb
66
+ - test/extra_attachment_test.rb
67
+ - test/validation_test.rb
68
+ - test/processors
69
+ - test/processors/image_science_test.rb
70
+ - test/processors/core_image_test.rb
71
+ - test/processors/rmagick_test.rb
72
+ - test/processors/gd2_test.rb
73
+ - test/processors/mini_magick_test.rb
74
+ - test/backends
75
+ - test/backends/file_system_test.rb
76
+ - test/backends/remote
77
+ - test/backends/remote/s3_test.rb
78
+ - test/backends/db_file_test.rb
79
+ - test/database.yml
80
+ - test/test_helper.rb
81
+ - test/geometry_test.rb
82
+ has_rdoc: false
83
+ homepage: http://weblog.techno-weenie.net
84
+ post_install_message:
85
+ rdoc_options: []
86
+
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ version:
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.2.0
105
+ signing_key:
106
+ specification_version: 2
107
+ summary: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
108
+ test_files:
109
+ - test/test_helper.rb