ncri_attachment_fu 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. data/CHANGELOG +35 -0
  2. data/LICENSE +20 -0
  3. data/README +193 -0
  4. data/amazon_s3.yml.tpl +17 -0
  5. data/lib/attachment_fu.rb +17 -0
  6. data/lib/geometry.rb +93 -0
  7. data/lib/technoweenie/attachment_fu.rb +533 -0
  8. data/lib/technoweenie/attachment_fu/backends.rb +4 -0
  9. data/lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb +211 -0
  10. data/lib/technoweenie/attachment_fu/backends/db_file_backend.rb +39 -0
  11. data/lib/technoweenie/attachment_fu/backends/file_system_backend.rb +126 -0
  12. data/lib/technoweenie/attachment_fu/backends/s3_backend.rb +394 -0
  13. data/lib/technoweenie/attachment_fu/processors.rb +1 -0
  14. data/lib/technoweenie/attachment_fu/processors/core_image_processor.rb +59 -0
  15. data/lib/technoweenie/attachment_fu/processors/gd2_processor.rb +54 -0
  16. data/lib/technoweenie/attachment_fu/processors/image_science_processor.rb +61 -0
  17. data/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb +132 -0
  18. data/lib/technoweenie/attachment_fu/processors/rmagick_processor.rb +57 -0
  19. data/rackspace_cloudfiles.yml.tpl +14 -0
  20. data/rails/install.rb +7 -0
  21. data/vendor/red_artisan/core_image/filters/color.rb +27 -0
  22. data/vendor/red_artisan/core_image/filters/effects.rb +31 -0
  23. data/vendor/red_artisan/core_image/filters/perspective.rb +25 -0
  24. data/vendor/red_artisan/core_image/filters/quality.rb +25 -0
  25. data/vendor/red_artisan/core_image/filters/scale.rb +47 -0
  26. data/vendor/red_artisan/core_image/filters/watermark.rb +32 -0
  27. data/vendor/red_artisan/core_image/processor.rb +123 -0
  28. metadata +75 -0
@@ -0,0 +1,25 @@
1
+ module RedArtisan
2
+ module CoreImage
3
+ module Filters
4
+ module Perspective
5
+
6
+ def perspective(top_left, top_right, bottom_left, bottom_right)
7
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
8
+
9
+ @original.perspective_transform :inputTopLeft => top_left, :inputTopRight => top_right, :inputBottomLeft => bottom_left, :inputBottomRight => bottom_right do |transformed|
10
+ @target = transformed
11
+ end
12
+ end
13
+
14
+ def perspective_tiled(top_left, top_right, bottom_left, bottom_right)
15
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
16
+
17
+ @original.perspective_tile :inputTopLeft => top_left, :inputTopRight => top_right, :inputBottomLeft => bottom_left, :inputBottomRight => bottom_right do |tiled|
18
+ @target = tiled
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module RedArtisan
2
+ module CoreImage
3
+ module Filters
4
+ module Quality
5
+
6
+ def reduce_noise(level = 0.02, sharpness = 0.4)
7
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
8
+
9
+ @original.noise_reduction :inputNoiseLevel => level, :inputSharpness => sharpness do |noise_reduced|
10
+ @target = noise_reduced
11
+ end
12
+ end
13
+
14
+ def adjust_exposure(input_ev = 0.5)
15
+ create_core_image_context(@original.extent.size.width, @original.extent.size.height)
16
+
17
+ @original.exposure_adjust :inputEV => input_ev do |adjusted|
18
+ @target = adjusted
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -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,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ncri_attachment_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jon Moses
9
+ - Nico Ritsche
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-02-10 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: attachment-fu for rails3. You know what it is.
16
+ email:
17
+ - jon@burningbush.us
18
+ - ncrdevmail@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/attachment_fu.rb
24
+ - lib/geometry.rb
25
+ - lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb
26
+ - lib/technoweenie/attachment_fu/backends/db_file_backend.rb
27
+ - lib/technoweenie/attachment_fu/backends/file_system_backend.rb
28
+ - lib/technoweenie/attachment_fu/backends/s3_backend.rb
29
+ - lib/technoweenie/attachment_fu/backends.rb
30
+ - lib/technoweenie/attachment_fu/processors/core_image_processor.rb
31
+ - lib/technoweenie/attachment_fu/processors/gd2_processor.rb
32
+ - lib/technoweenie/attachment_fu/processors/image_science_processor.rb
33
+ - lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb
34
+ - lib/technoweenie/attachment_fu/processors/rmagick_processor.rb
35
+ - lib/technoweenie/attachment_fu/processors.rb
36
+ - lib/technoweenie/attachment_fu.rb
37
+ - rails/install.rb
38
+ - vendor/red_artisan/core_image/filters/color.rb
39
+ - vendor/red_artisan/core_image/filters/effects.rb
40
+ - vendor/red_artisan/core_image/filters/perspective.rb
41
+ - vendor/red_artisan/core_image/filters/quality.rb
42
+ - vendor/red_artisan/core_image/filters/scale.rb
43
+ - vendor/red_artisan/core_image/filters/watermark.rb
44
+ - vendor/red_artisan/core_image/processor.rb
45
+ - CHANGELOG
46
+ - LICENSE
47
+ - README
48
+ - amazon_s3.yml.tpl
49
+ - rackspace_cloudfiles.yml.tpl
50
+ homepage: https://github.com/ncri/attachment_fu
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.24
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Attachment-fu for rails3
74
+ test_files: []
75
+ has_rdoc: