forked from willbryant/attachment_saver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
139 lines (107 loc) · 5.58 KB
/
README
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
AttachmentSaver
===============
This plugin implements attachment storage and processing, integrated with
ActiveRecord models and Ruby CGI/Rails-style uploads. Image processing
operations including a number of different resizing & thumbnailing modes are
provided, and the architecture simplifies clean implementation of other types
of processing. Errors are carefully handled to minimize the possibility of
broken uploads leaving incomplete or corrupt data.
RMagick, MiniMagick, ImageScience, and GdkPixbuf image processors are supported.
The pure-ruby ImageSize image processor is also supported (for inspecting images but not resizing them).
Compatibility
=============
Currently tested against Rails 5.2 (up to 5.2.0.beta2), 5.1 (up to 5.1.4), 5.0 (up to 5.0.6), and 4.2 (up to 4.2.10), on Ruby 2.3.6, 2.4.3, and 2.5.0.
Was also tested compatible with 2.3.14, 3.0.17, 3.1.8, 3.2.13, 4.2, and 5.0.
Examples
========
A 'dumb' attachment store that saves minimal info
-------------------------------------------------
# in your model:
class SomeModel
saves_attachment
end
# in your database schema:
create_table :some_model do |t|
t.string :storage_key, :null => false
end
# in your new/update forms:
file_field :some_model, :uploaded_data
# no special controller handling is required.
A 'dumb' attachment store that saves full file info automatically
-----------------------------------------------------------------
# as for above, but in the schema:
create_table :some_model do |t|
t.string :storage_key, :null => false
t.string :original_filename, :null => false # as sent by the user's browser, with IE path removed
t.string :content_type, :null => false # as sent by the user's browser
t.integer :size, :null => false # file size in bytes
t.timestamps
end
An image store that automatically saves width and height and corrects mime types & file extensions
--------------------------------------------------------------------------------------------------
# in your models:
class Image
saves_attachment :processor => 'rmagick'
end
# in your database schema:
create_table :photos do |t|
t.string :storage_key, :null => false
t.string :original_filename, :null => false # as sent by the user's browser, with IE path removed
t.string :content_type, :null => false # corrected if the user's browser sent a mime type that didn't match the image
t.integer :size, :null => false # file size in bytes
t.integer :width, :null => false # set by the image processors
t.integer :height, :null => false # ditto
t.timestamps
end
An image store that resizes images to produce thumbnails etc.
-------------------------------------------------------------
# in your models:
class Photo
saves_attachment :processor => 'RMagick', :derived_class => 'Thumbnail',
:formats => {:page_width => '520x', # ImageMagick-style format string
:small => [:shrink_to_fit, 250, 250], # or more explicit [operation, width, height] format
:nav => [:cover_and_crop, 50, 50]} # lots of useful resize and/or crop modes available
end
class Thumbnail
saves_attachment
end
# in your database schema:
create_table :photos do |t|
t.string :storage_key, :null => false
t.string :original_filename, :null => false # as sent by the user's browser, with IE path removed
t.string :content_type, :null => false # corrected if the user's browser sent a mime type that didn't match the image
t.integer :size, :null => false # file size in bytes
t.integer :width, :null => false # set by the image processors
t.integer :height, :null => false # ditto
t.timestamps
end
create_table :thumbnails do |t|
t.string :original_type, :null => false # multiple models can save their derived images as thumbnails
t.integer :original_id, :null => false
t.string :format_name, :null => false # from your :formats - eg. 'small', 'nav'
t.string :storage_key, :null => false # still required (but will be based on the original's, for convenience)
t.string :content_type, :null => false # these fields are optional (as they are for Photo)
t.integer :size, :null => false
t.integer :width, :null => false # but width and height are generally needed for layout
t.integer :height, :null => false
t.timestamps
end
A custom image-processing format using your image-processor's features
----------------------------------------------------------------------
# in a file in your lib/ directory that's required in somewhere:
module AttachmentSaver::Processors::RMagick::Operations # or MiniMagick::Operations or ImageScience::Operations or GdkPixbuf::Operations - see lib/processors
# this module is mixed in to the actual image objects built by the processor, so you can call its' methods directly
def wavy_black_and_white(wave_height, wave_length, &block)
# RMagick returns the new object; MiniMagick acts on the same object (so you must dup); ImageScience yields; so, look at the existing lib/processors to see the appropriate pattern
image = quantize(256, Magick::GRAYColorspace).wave(wave_height, wave_length)
# mix the operations in to the new image, for reuse
image.extend Operations
# yield up the new image
block.call(image)
end
end
# in your models:
class Image
saves_attachment :processor => 'RMagick', :derived_class => 'SpecialImage',
:formats => {:flashback => [:wavy_and_black_and_white, 10, 200]}
end