-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathREADME
More file actions
78 lines (52 loc) · 2.19 KB
/
README
File metadata and controls
78 lines (52 loc) · 2.19 KB
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
Requirements:
imagemagick - in particular the "identify" command
Include CarrierWave::ImageSize in your CarrierWave uploader
to have image sizes and content type automatically stored
along with the image. For this to work, you must create a
second column in your database named:
"#{image_name}_information"
For instance, if your image is named "pic", you need to create
a string or text column named "pic_information".
t.string :pic, :limit => 250
t.string :pic_information, :limit => 250
In your model, you mount the uploader
mount_uploader :pic, PicUploader
And in the uploader, you include CarrierWave::ImageSize:
class LogoUploader < CarrierWave::Uploader::Base
...
include CarrierWave::ImageSize
Note that you might also need to "require" it at the top
of the file:
require 'carrier_wave/image_size'
You now have three new methods on your uploaded pictures:
image_width, image_height - returns height and width of images
as integers
content_type - returns the original content type of the image
Note that nested versions are fully supported and you can use
these methods with any of your versions:
version :thumb do
process :resize_to_fit => [50, 50]
version :tiny do
process :resize_to_fit => [10, 10]
end
end
In your code:
artist.pic.thumb.tiny.image_width
Additionally, your form should propogate the _information field
along with the cache, otherwise the content type will be lost.
<%= f.file_field :pic %>
<%= f.hidden_field :pic_cache %>
<%= f.hidden_field :pic_information unless f.object.pic_cache.blank? %>
The only limitation is that you cannot create a version of your
picture called "base", as I use that for storing the size of the
base image. If you really badly need to use "base", search for "base"
below and change it to something else.
Internally, the _information field is simply a string:
"image/png\nbase:200x150\nthumb:20x15\nthumb_tiny:10x7\n"
Where the sizes are simply width x height. The content_type
is saved across "recreate_versions!".
Improvements (TODO):
1. This should be turned into a gem
2. Could probably better use the image processor (i.e. minimagick)
to get size information
3. Needs some tests