Class: Cropper::Upload

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cropper/upload.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#multiplierObject

Returns the value of attribute multiplier.



11
12
13
# File 'app/models/cropper/upload.rb', line 11

def multiplier
  @multiplier
end

#reprocessedObject

Returns the value of attribute reprocessed.



11
12
13
# File 'app/models/cropper/upload.rb', line 11

def reprocessed
  @reprocessed
end

Instance Method Details

#crop_changed?Boolean

crop_changed? returns true if any property has changed that should cause a recrop. That shuold include the file attachment itself.

Returns:

  • (Boolean)


56
57
58
# File 'app/models/cropper/upload.rb', line 56

def crop_changed?
  !self.reprocessed && !![:scale_width, :scale_height, :offset_top, :offset_left, :holder_type, :holder_id, :holder_column].detect { |col| self.send :"#{col}_changed?" }
end

#crop_geometryObject

Cropped geometry is always to a fixed size, so we can just return parts of the definition knowing that they match the eventual dimensions. Useful, because we need this information to build the cropping interface.



92
93
94
# File 'app/models/cropper/upload.rb', line 92

def crop_geometry
  @crop_geometry ||= Cropper.crop_geometry(holder_type, holder_column)
end

#crop_heightObject



100
101
102
# File 'app/models/cropper/upload.rb', line 100

def crop_height
  @cropped_height ||= crop_geometry.split('x').last.to_i
end

#crop_widthObject



96
97
98
# File 'app/models/cropper/upload.rb', line 96

def crop_width
  @cropped_width ||= crop_geometry.split('x').first.to_i
end

#dimensions_known?Boolean

dimensions_known? returns true we have managed to discover the dimensions of the original file.

Returns:

  • (Boolean)


165
166
167
# File 'app/models/cropper/upload.rb', line 165

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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/cropper/upload.rb', line 116

def geometry(style_name='original')
  @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.



141
142
143
# File 'app/models/cropper/upload.rb', line 141

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.

Returns:

  • (Boolean)


159
160
161
# File 'app/models/cropper/upload.rb', line 159

def horizontal?(style_name='original')
  geometry(style_name).horizontal?
end

#original_geometryObject

original_geometry returns the discovered dimensions of the uploaded file as a paperclip geometry object.



106
107
108
# File 'app/models/cropper/upload.rb', line 106

def original_geometry
  @original_geometry ||= Paperclip::Geometry.new(original_width, original_height)
end

#paperclip_stylesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/cropper/upload.rb', line 30

def paperclip_styles
  styles = {}
  styles[:precrop] = {
    :geometry => precrop_geometry,
    :processors => [:thumbnail]
  }
  if croppable?
    styles[:cropped] = {
      :geometry => crop_geometry,
      :processors => [:offset_thumbnail],
      :scale => "#{scale_width}x",
      :crop_and_offset => "%dx%d%+d%+d" % [crop_width, crop_height, -offset_left, -offset_top]
    }
  end
  styles
end

#precrop_geometryObject

## Crop boundaries

Precrop geometry is unpredictable and has to be calculated.



76
77
78
# File 'app/models/cropper/upload.rb', line 76

def precrop_geometry
  @precrop_geometry ||= Cropper.precrop_geometry(holder_type, holder_column)
end

#precrop_heightObject



84
85
86
# File 'app/models/cropper/upload.rb', line 84

def precrop_height
  @precrop_height ||= height(:precrop)
end

#precrop_widthObject



80
81
82
# File 'app/models/cropper/upload.rb', line 80

def precrop_width
  @precrop_width ||= width(:precrop)
end

#reprocess_if_crop_changedObject



60
61
62
# File 'app/models/cropper/upload.rb', line 60

def reprocess_if_crop_changed
  self.file.assign(file) if crop_changed?
end

#square?(style_name = 'original') ⇒ Boolean

square? returns true if width and height are the same.

Returns:

  • (Boolean)


147
148
149
# File 'app/models/cropper/upload.rb', line 147

def square?(style_name='original')
  geometry(style_name).square?
end

#styled_file(style = :cropped) ⇒ Object



47
48
49
50
51
52
# File 'app/models/cropper/upload.rb', line 47

def styled_file(style=:cropped)
  settings = Rails.application.config.paperclip_defaults
  if bucket = Fog::Storage.new(settings[:fog_credentials]).directories.get(settings[:fog_directory])
    bucket.files.get(file.path(style))
  end
end

#url(style = :cropped) ⇒ Object



171
172
173
# File 'app/models/cropper/upload.rb', line 171

def url(style=:cropped)
  file.url(:style)
end

#vertical?(style_name = 'original') ⇒ Boolean

vertical? returns true if the image, in the given style, is taller than it is wide.

Returns:

  • (Boolean)


153
154
155
# File 'app/models/cropper/upload.rb', line 153

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.



135
136
137
# File 'app/models/cropper/upload.rb', line 135

def width(style_name='original')
  geometry(style_name).width.to_i
end