pothoven-attachment_fu 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +59 -0
- data/LICENSE +20 -0
- data/README +253 -0
- data/Rakefile +22 -0
- data/amazon_s3.yml.tpl +17 -0
- data/install.rb +7 -0
- data/lib/geometry.rb +96 -0
- data/lib/pothoven-attachment_fu.rb +21 -0
- data/lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb +211 -0
- data/lib/technoweenie/attachment_fu/backends/db_file_backend.rb +39 -0
- data/lib/technoweenie/attachment_fu/backends/file_system_backend.rb +126 -0
- data/lib/technoweenie/attachment_fu/backends/s3_backend.rb +394 -0
- data/lib/technoweenie/attachment_fu/processors/core_image_processor.rb +66 -0
- data/lib/technoweenie/attachment_fu/processors/gd2_processor.rb +59 -0
- data/lib/technoweenie/attachment_fu/processors/image_science_processor.rb +80 -0
- data/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb +142 -0
- data/lib/technoweenie/attachment_fu/processors/rmagick_processor.rb +84 -0
- data/lib/technoweenie/attachment_fu.rb +577 -0
- data/rackspace_cloudfiles.yml.tpl +14 -0
- data/test/backends/db_file_test.rb +16 -0
- data/test/backends/file_system_test.rb +143 -0
- data/test/backends/remote/cloudfiles_test.rb +102 -0
- data/test/backends/remote/s3_test.rb +119 -0
- data/test/base_attachment_tests.rb +77 -0
- data/test/basic_test.rb +120 -0
- data/test/database.yml +18 -0
- data/test/extra_attachment_test.rb +67 -0
- data/test/fixtures/attachment.rb +304 -0
- data/test/fixtures/files/fake/rails.png +0 -0
- data/test/fixtures/files/foo.txt +1 -0
- data/test/fixtures/files/rails.jpg +0 -0
- data/test/fixtures/files/rails.png +0 -0
- data/test/geometry_test.rb +114 -0
- data/test/processors/core_image_test.rb +58 -0
- data/test/processors/gd2_test.rb +51 -0
- data/test/processors/image_science_test.rb +54 -0
- data/test/processors/mini_magick_test.rb +122 -0
- data/test/processors/rmagick_test.rb +272 -0
- data/test/schema.rb +136 -0
- data/test/test_helper.rb +180 -0
- data/test/validation_test.rb +55 -0
- data/vendor/red_artisan/core_image/filters/color.rb +27 -0
- data/vendor/red_artisan/core_image/filters/effects.rb +31 -0
- data/vendor/red_artisan/core_image/filters/perspective.rb +25 -0
- data/vendor/red_artisan/core_image/filters/quality.rb +25 -0
- data/vendor/red_artisan/core_image/filters/scale.rb +47 -0
- data/vendor/red_artisan/core_image/filters/watermark.rb +32 -0
- data/vendor/red_artisan/core_image/processor.rb +123 -0
- metadata +97 -0
data/test/test_helper.rb
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
|
2
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
3
|
+
|
4
|
+
ENV['RAILS_ENV'] = 'test'
|
5
|
+
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
|
9
|
+
|
10
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
11
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
12
|
+
|
13
|
+
db_adapter = ENV['DB']
|
14
|
+
|
15
|
+
# no db passed, try one of these fine config-free DBs before bombing.
|
16
|
+
db_adapter ||=
|
17
|
+
begin
|
18
|
+
require 'rubygems'
|
19
|
+
require 'sqlite'
|
20
|
+
'sqlite'
|
21
|
+
rescue MissingSourceFile
|
22
|
+
begin
|
23
|
+
require 'sqlite3'
|
24
|
+
'sqlite3'
|
25
|
+
rescue MissingSourceFile
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
if db_adapter.nil?
|
30
|
+
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Base.establish_connection(config[db_adapter])
|
34
|
+
|
35
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
36
|
+
|
37
|
+
FIXTURE_PATH = File.dirname(__FILE__) + "/fixtures"
|
38
|
+
$LOAD_PATH.unshift(FIXTURE_PATH)
|
39
|
+
|
40
|
+
class Test::Unit::TestCase #:nodoc:
|
41
|
+
include ActionDispatch::TestProcess
|
42
|
+
def create_fixtures(*table_names)
|
43
|
+
if block_given?
|
44
|
+
Fixtures.create_fixtures(FIXTURE_PATH, table_names) { yield }
|
45
|
+
else
|
46
|
+
Fixtures.create_fixtures(FIXTURE_PATH, table_names)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def setup
|
51
|
+
Attachment.saves = 0
|
52
|
+
DbFile.transaction { [Attachment, FileAttachment, OrphanAttachment, MinimalAttachment, DbFile].each { |klass| klass.delete_all } }
|
53
|
+
attachment_model self.class.attachment_model
|
54
|
+
end
|
55
|
+
|
56
|
+
def teardown
|
57
|
+
FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
|
58
|
+
# Files generated by random_tempfile_filename
|
59
|
+
FileUtils.rm_rf Dir['[0-9]*.{png,jpg}']
|
60
|
+
end
|
61
|
+
|
62
|
+
#self.use_transactional_fixtures = true
|
63
|
+
#self.use_instantiated_fixtures = false
|
64
|
+
|
65
|
+
def self.attachment_model(klass = nil)
|
66
|
+
@attachment_model = klass if klass
|
67
|
+
@attachment_model
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.test_against_class(test_method, klass, subclass = false)
|
71
|
+
define_method("#{test_method}_on_#{:sub if subclass}class") do
|
72
|
+
klass = Class.new(klass) if subclass
|
73
|
+
attachment_model klass
|
74
|
+
send test_method, klass
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.test_against_subclass(test_method, klass)
|
79
|
+
test_against_class test_method, klass, true
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
def upload_file(options = {})
|
84
|
+
use_temp_file options[:filename] do |file|
|
85
|
+
opts = { :uploaded_data => fixture_file_upload(file, options[:content_type] || 'image/png') }
|
86
|
+
opts.update(options.reject { |k, v| ![:imageable_type, :imageable_id].include?(k) })
|
87
|
+
att = attachment_model.create opts
|
88
|
+
att.reload unless att.new_record?
|
89
|
+
return att
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def upload_merb_file(options = {})
|
94
|
+
use_temp_file options[:filename] do |file|
|
95
|
+
att = attachment_model.create :uploaded_data => {"size" => file.size, "content_type" => options[:content_type] || 'image/png', "filename" => file, 'tempfile' => fixture_file_upload(file, options[:content_type] || 'image/png')}
|
96
|
+
att.reload unless att.new_record?
|
97
|
+
return att
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def use_temp_file(fixture_filename)
|
102
|
+
temp_path = File.join('/tmp', File.basename(fixture_filename))
|
103
|
+
temp_dir = File.join(FIXTURE_PATH, 'tmp')
|
104
|
+
use_file = File.join(FIXTURE_PATH, temp_path)
|
105
|
+
FileUtils.mkdir_p temp_dir
|
106
|
+
FileUtils.cp File.join(FIXTURE_PATH, fixture_filename), use_file
|
107
|
+
yield use_file
|
108
|
+
ensure
|
109
|
+
FileUtils.rm_rf temp_dir
|
110
|
+
end
|
111
|
+
|
112
|
+
def assert_created(num = 1)
|
113
|
+
assert_difference attachment_model.base_class, :count, num do
|
114
|
+
if attachment_model.included_modules.include? DbFile
|
115
|
+
assert_difference DbFile, :count, num do
|
116
|
+
yield
|
117
|
+
end
|
118
|
+
else
|
119
|
+
yield
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def assert_valid(record)
|
125
|
+
assert record.valid?, record.errors.full_messages.join("\n")
|
126
|
+
end
|
127
|
+
|
128
|
+
def assert_file_jpeg_quality(model, thumbnail, expected)
|
129
|
+
filename = if model.respond_to?(:full_filename)
|
130
|
+
model.full_filename(thumbnail)
|
131
|
+
else
|
132
|
+
thumb = thumbnail ? model.thumbnails.find(:first, :conditions => { :thumbnail => thumbnail.to_s }, :include => :db_file) : model
|
133
|
+
unless thumb && thumb.db_file && thumb.db_file.data && thumb.db_file.data.size > 0
|
134
|
+
STDERR.puts "Cannot find DB file data for thumbnail #{thumbnail.inspect} -> Aborting JPEG quality check."
|
135
|
+
return
|
136
|
+
end
|
137
|
+
result = Tempfile.new('dbfile_dump').path
|
138
|
+
File.open(result, 'wb') { |f| f.write(thumb.db_file.data) }
|
139
|
+
result
|
140
|
+
end
|
141
|
+
quality = %x(identify -format '%Q' "#{filename}" 2> /dev/null)
|
142
|
+
if $?.success?
|
143
|
+
assert_equal expected, quality.to_i, "Produced JPEG quality (thumbnail: #{thumbnail.inspect}) is incorrect."
|
144
|
+
else
|
145
|
+
STDERR.puts "ImageMagick's identify not found / not in PATH: can't quickly check produced image quality."
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def assert_not_created
|
150
|
+
assert_created(0) { yield }
|
151
|
+
end
|
152
|
+
|
153
|
+
def should_reject_by_size_with(klass)
|
154
|
+
attachment_model klass
|
155
|
+
assert_not_created do
|
156
|
+
attachment = upload_file :filename => '/files/rails.png'
|
157
|
+
assert attachment.new_record?
|
158
|
+
assert attachment.errors.on(:size)
|
159
|
+
assert_nil attachment.db_file if attachment.respond_to?(:db_file)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def assert_difference(object, method = nil, difference = 1)
|
164
|
+
initial_value = object.send(method)
|
165
|
+
yield
|
166
|
+
assert_equal initial_value + difference, object.send(method)
|
167
|
+
end
|
168
|
+
|
169
|
+
def assert_no_difference(object, method, &block)
|
170
|
+
assert_difference object, method, 0, &block
|
171
|
+
end
|
172
|
+
|
173
|
+
def attachment_model(klass = nil)
|
174
|
+
@attachment_model = klass if klass
|
175
|
+
@attachment_model
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
require File.join(File.dirname(__FILE__), 'fixtures/attachment')
|
180
|
+
require File.join(File.dirname(__FILE__), 'base_attachment_tests')
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
|
+
|
3
|
+
class ValidationTest < Test::Unit::TestCase
|
4
|
+
def test_should_invalidate_big_files
|
5
|
+
@attachment = SmallAttachment.new
|
6
|
+
assert !@attachment.valid?
|
7
|
+
assert @attachment.errors[:size]
|
8
|
+
|
9
|
+
@attachment.size = 2000
|
10
|
+
assert !@attachment.valid?
|
11
|
+
assert @attachment.errors[:size], @attachment.errors.full_messages.to_sentence
|
12
|
+
|
13
|
+
@attachment.size = 1000
|
14
|
+
assert !@attachment.valid?
|
15
|
+
assert @attachment.errors[:size].empty?
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_invalidate_small_files
|
19
|
+
@attachment = BigAttachment.new
|
20
|
+
assert !@attachment.valid?
|
21
|
+
assert @attachment.errors[:size]
|
22
|
+
|
23
|
+
@attachment.size = 2000
|
24
|
+
assert !@attachment.valid?
|
25
|
+
assert @attachment.errors[:size], @attachment.errors.full_messages.to_sentence
|
26
|
+
|
27
|
+
@attachment.size = 1.megabyte
|
28
|
+
assert !@attachment.valid?
|
29
|
+
assert @attachment.errors[:size].empty?
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_validate_content_type
|
33
|
+
@attachment = PdfAttachment.new
|
34
|
+
assert !@attachment.valid?
|
35
|
+
assert @attachment.errors[:content_type]
|
36
|
+
|
37
|
+
@attachment.content_type = 'foo'
|
38
|
+
assert !@attachment.valid?
|
39
|
+
assert @attachment.errors[:content_type]
|
40
|
+
|
41
|
+
@attachment.content_type = 'pdf'
|
42
|
+
assert !@attachment.valid?
|
43
|
+
assert @attachment.errors[:content_type].empty?
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_require_filename
|
47
|
+
@attachment = Attachment.new
|
48
|
+
assert !@attachment.valid?
|
49
|
+
assert @attachment.errors[:filename]
|
50
|
+
|
51
|
+
@attachment.filename = 'foo'
|
52
|
+
assert !@attachment.valid?
|
53
|
+
assert @attachment.errors[:filename].empty?
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RedArtisan
|
2
|
+
module CoreImage
|
3
|
+
module Filters
|
4
|
+
module Color
|
5
|
+
|
6
|
+
def greyscale(color = nil, intensity = 1.00)
|
7
|
+
create_core_image_context(@original.extent.size.width, @original.extent.size.height)
|
8
|
+
|
9
|
+
color = OSX::CIColor.colorWithString("1.0 1.0 1.0 1.0") unless color
|
10
|
+
|
11
|
+
@original.color_monochrome :inputColor => color, :inputIntensity => intensity do |greyscale|
|
12
|
+
@target = greyscale
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def sepia(intensity = 1.00)
|
17
|
+
create_core_image_context(@original.extent.size.width, @original.extent.size.height)
|
18
|
+
|
19
|
+
@original.sepia_tone :inputIntensity => intensity do |sepia|
|
20
|
+
@target = sepia
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RedArtisan
|
2
|
+
module CoreImage
|
3
|
+
module Filters
|
4
|
+
module Effects
|
5
|
+
|
6
|
+
def spotlight(position, points_at, brightness, concentration, color)
|
7
|
+
create_core_image_context(@original.extent.size.width, @original.extent.size.height)
|
8
|
+
|
9
|
+
@original.spot_light :inputLightPosition => vector3(*position), :inputLightPointsAt => vector3(*points_at),
|
10
|
+
:inputBrightness => brightness, :inputConcentration => concentration, :inputColor => color do |spot|
|
11
|
+
@target = spot
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def edges(intensity = 1.00)
|
16
|
+
create_core_image_context(@original.extent.size.width, @original.extent.size.height)
|
17
|
+
|
18
|
+
@original.edges :inputIntensity => intensity do |edged|
|
19
|
+
@target = edged
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def vector3(x, y, w)
|
26
|
+
OSX::CIVector.vectorWithX_Y_Z(x, y, w)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -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,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pothoven-attachment_fu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rick Olson
|
9
|
+
- Steven Pothoven
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-10-26 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: This is a fork of Rick Olson’s attachment_fu adding Ruby 1.9 and Rails
|
16
|
+
3.2 support as well as some other enhancements.
|
17
|
+
email: steven@pothoven.net
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files:
|
21
|
+
- README
|
22
|
+
files:
|
23
|
+
- CHANGELOG
|
24
|
+
- LICENSE
|
25
|
+
- README
|
26
|
+
- Rakefile
|
27
|
+
- install.rb
|
28
|
+
- amazon_s3.yml.tpl
|
29
|
+
- rackspace_cloudfiles.yml.tpl
|
30
|
+
- lib/geometry.rb
|
31
|
+
- lib/pothoven-attachment_fu.rb
|
32
|
+
- lib/technoweenie/attachment_fu/backends/cloud_file_backend.rb
|
33
|
+
- lib/technoweenie/attachment_fu/backends/db_file_backend.rb
|
34
|
+
- lib/technoweenie/attachment_fu/backends/file_system_backend.rb
|
35
|
+
- lib/technoweenie/attachment_fu/backends/s3_backend.rb
|
36
|
+
- lib/technoweenie/attachment_fu/processors/core_image_processor.rb
|
37
|
+
- lib/technoweenie/attachment_fu/processors/gd2_processor.rb
|
38
|
+
- lib/technoweenie/attachment_fu/processors/image_science_processor.rb
|
39
|
+
- lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb
|
40
|
+
- lib/technoweenie/attachment_fu/processors/rmagick_processor.rb
|
41
|
+
- lib/technoweenie/attachment_fu.rb
|
42
|
+
- test/base_attachment_tests.rb
|
43
|
+
- test/basic_test.rb
|
44
|
+
- test/database.yml
|
45
|
+
- test/extra_attachment_test.rb
|
46
|
+
- test/geometry_test.rb
|
47
|
+
- test/schema.rb
|
48
|
+
- test/test_helper.rb
|
49
|
+
- test/validation_test.rb
|
50
|
+
- test/backends/db_file_test.rb
|
51
|
+
- test/backends/file_system_test.rb
|
52
|
+
- test/backends/remote/cloudfiles_test.rb
|
53
|
+
- test/backends/remote/s3_test.rb
|
54
|
+
- test/fixtures/attachment.rb
|
55
|
+
- test/fixtures/files/foo.txt
|
56
|
+
- test/fixtures/files/rails.jpg
|
57
|
+
- test/fixtures/files/rails.png
|
58
|
+
- test/fixtures/files/fake/rails.png
|
59
|
+
- test/processors/core_image_test.rb
|
60
|
+
- test/processors/gd2_test.rb
|
61
|
+
- test/processors/image_science_test.rb
|
62
|
+
- test/processors/mini_magick_test.rb
|
63
|
+
- test/processors/rmagick_test.rb
|
64
|
+
- vendor/red_artisan/core_image/processor.rb
|
65
|
+
- vendor/red_artisan/core_image/filters/color.rb
|
66
|
+
- vendor/red_artisan/core_image/filters/effects.rb
|
67
|
+
- vendor/red_artisan/core_image/filters/perspective.rb
|
68
|
+
- vendor/red_artisan/core_image/filters/quality.rb
|
69
|
+
- vendor/red_artisan/core_image/filters/scale.rb
|
70
|
+
- vendor/red_artisan/core_image/filters/watermark.rb
|
71
|
+
homepage: http://github.com/pothoven/attachment_fu
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options:
|
75
|
+
- --inline-source
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project: nowarning
|
93
|
+
rubygems_version: 1.8.24
|
94
|
+
signing_key:
|
95
|
+
specification_version: 2
|
96
|
+
summary: attachment_fu as a gem
|
97
|
+
test_files: []
|