Class: ImageUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
CarrierWave::MiniMagick
Defined in:
app/uploaders/image_uploader.rb

Instance Method Summary collapse

Instance Method Details

#cache_dirObject

Override the directory where uploaded files will be stored. This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/#Rails.env/#ImageUploader.modelmodel.classmodel.class.to_smodel.class.to_s.underscore/#ImageUploader.modelmodel.id" end



19
20
21
# File 'app/uploaders/image_uploader.rb', line 19

def cache_dir
  Rails.root.join 'tmp/uploads'
end

#cropObject

version :tiny do process :resize_to_fit => [25, 25] end



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/uploaders/image_uploader.rb', line 47

def crop
  if model.crop_x.present?
    resize_to_limit(540, 540)
    manipulate! do |img|
      x = model.crop_x.to_i
      y = model.crop_y.to_i
      w = model.crop_w.to_i
      h = model.crop_h.to_i
      img.crop!(x, y, w, h)
    end
  end
end

#extension_whitelistObject

Add a white list of extensions which are allowed to be uploaded. For images you might use something like this:



94
95
96
# File 'app/uploaders/image_uploader.rb', line 94

def extension_whitelist
  %w(jpg jpeg png)
end

#optimizeObject



60
61
62
63
64
65
66
67
68
69
70
# File 'app/uploaders/image_uploader.rb', line 60

def optimize
  manipulate! do |img|
    img.combine_options do |c|
      c.strip
      c.quality '85'
      c.depth '8'
      c.interlace 'Line'
    end
    img
  end
end