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
|
# File 'lib/voltron/crop/active_record/base.rb', line 9
def mount_uploader(*args)
super *args
column = args.first.to_sym
attr_accessor "#{column}_x", "#{column}_y", "#{column}_w", "#{column}_h", "#{column}_zoom"
after_validation do
upload = send(column)
if errors.empty? && upload.present?
to = Tempfile.new([column.to_s, File.extname(upload.path)])
dimensions = [send("#{column}_w"), send("#{column}_w")].join('x')
coordinates = [send("#{column}_x"), send("#{column}_y")].join('+')
img = upload.url.starts_with?('http') ? ::MiniMagick::Image.open(upload.url) : ::MiniMagick::Image.open(upload.path)
img.crop("#{dimensions}+#{coordinates}")
if img.width < Voltron.config.crop.min_width || img.height < Voltron.config.crop.min_height
img.resize "#{Voltron.config.crop.min_width}x#{Voltron.config.crop.min_height}"
end
img.write to.path
to.close
input_name = ActionView::Helpers::Tags::Base.new(ActiveModel::Naming.param_key(self), column, nil).send(:tag_name)
file = ActionDispatch::Http::UploadedFile.new({
type: upload.content_type,
filename: upload.file.filename,
head: "Content-Disposition: form-data; name=\"#{input_name}\"; filename=\"#{upload.filename}\"\r\nContent-Type: #{upload.content_type}\r\n",
tempfile: to
})
send("#{column}=", file)
end
end
end
|