Class: Upload
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Upload
- Defined in:
- app/models/upload.rb
Overview
This is a standard upload class that should be useable for most purposes. We assume that even when the final destination is an S3 bucket, the initial upload will be held locally.
Instance Method Summary collapse
-
#dimensions_known? ⇒ Boolean
dimensions_known? returns true we have managed to discover the dimensions of the original file.
-
#geometry(style_name = 'original') ⇒ Object
geometry, given a style name, returns the dimensions of the file if that style were applied.
-
#height(style_name = 'original') ⇒ Object
height returns the height of this image in a given style.
-
#horizontal?(style_name = 'original') ⇒ Boolean
horizontal? returns true if the image, in the given style, is wider than it is tall.
-
#original_geometry ⇒ Object
original_geometry returns the discovered dimensions of the uploaded file as a paperclip geometry object.
- #precrop_processors ⇒ Object
-
#precrop_styles ⇒ Object
To change precrop dimensions or other thumbnail properties, just monkeypatch this method.
-
#square?(style_name = 'original') ⇒ Boolean
square? returns true if width and height are the same.
-
#vertical?(style_name = 'original') ⇒ Boolean
vertical? returns true if the image, in the given style, is taller than it is wide.
-
#width(style_name = 'original') ⇒ Object
width returns the width of this image in a given style.
Instance Method Details
#dimensions_known? ⇒ Boolean
dimensions_known? returns true we have managed to discover the dimensions of the original file.
99 100 101 |
# File 'app/models/upload.rb', line 99 def dimensions_known? original_width? && original_height? end |
#geometry(style_name = 'original') ⇒ Object
geometry, given a style name, returns the dimensions of the file if that style were applied. For speed we calculate this rather than reading the file, which might be in S3 or some other distant place.
The logic is in [lib/paperclip/geometry_tranformation.rb](/lib/paperclip/geometry_tranformation.html), which is a ruby library that mimics the action of imagemagick’s convert command.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/models/upload.rb', line 49 def geometry(style_name='original') # These calculations are all memoised. @geometry ||= {} begin @geometry[style_name] ||= if style_name.to_s == 'original' # If no style name is given, or it is 'original', we return the original discovered dimensions. original_geometry else # Otherwise, we apply a mock transformation to see what dimensions would result. style = self.file.styles[style_name.to_sym] original_geometry.transformed_by(style.geometry) end rescue Paperclip::TransformationError => e # In case of explosion, we always return the original dimensions so that action can continue. original_geometry end end |
#height(style_name = 'original') ⇒ Object
height returns the height of this image in a given style.
75 76 77 |
# File 'app/models/upload.rb', line 75 def height(style_name='original') geometry(style_name).height.to_i end |
#horizontal?(style_name = 'original') ⇒ Boolean
horizontal? returns true if the image, in the given style, is wider than it is tall.
93 94 95 |
# File 'app/models/upload.rb', line 93 def horizontal?(style_name='original') geometry(style_name).horizontal? end |
#original_geometry ⇒ Object
original_geometry returns the discovered dimensions of the uploaded file as a paperclip geometry object.
39 40 41 |
# File 'app/models/upload.rb', line 39 def original_geometry @original_geometry ||= Paperclip::Geometry.new(original_width, original_height) end |
#precrop_processors ⇒ Object
24 25 26 |
# File 'app/models/upload.rb', line 24 def precrop_processors [:thumbnail] end |
#precrop_styles ⇒ Object
To change precrop dimensions or other thumbnail properties, just monkeypatch this method.
16 17 18 19 20 21 22 |
# File 'app/models/upload.rb', line 16 def precrop_styles { :icon => { :geometry => "40x40#" }, :thumb => { :geometry => "100x100#" }, :precrop => { :geometry => "1600x3000" } } end |
#square?(style_name = 'original') ⇒ Boolean
square? returns true if width and height are the same.
81 82 83 |
# File 'app/models/upload.rb', line 81 def square?(style_name='original') geometry(style_name).square? end |
#vertical?(style_name = 'original') ⇒ Boolean
vertical? returns true if the image, in the given style, is taller than it is wide.
87 88 89 |
# File 'app/models/upload.rb', line 87 def vertical?(style_name='original') geometry(style_name).vertical? end |
#width(style_name = 'original') ⇒ Object
width returns the width of this image in a given style.
69 70 71 |
# File 'app/models/upload.rb', line 69 def width(style_name='original') geometry(style_name).width.to_i end |