-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad_image_uploader.rb
More file actions
85 lines (70 loc) · 2.09 KB
/
ad_image_uploader.rb
File metadata and controls
85 lines (70 loc) · 2.09 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
79
80
81
82
83
84
85
class AdImageUploader < BaseUploader
INDEX_BY_SIZE = { '300x250': 0, '300x150': 1, '300x90': 2, '250x90': 3 }.freeze
process :quality => 90
version :cropped do
process :crop, if: :crop_needed?
process resize_to_limit: [800, 800]
end
version :list, from_version: :cropped do
process resize_to_fill_gif: [60, 60]
end
version :carousel, from_version: :cropped do
process resize_to_fill_gif: [260, 130]
end
version :size_300x250 do
process crop: INDEX_BY_SIZE[:'300x250'], if: :crop_needed?
process resize_to_fill_gif: [298, 248]
end
version :size_300x150 do
process crop: INDEX_BY_SIZE[:'300x150'], if: :crop_needed?
process resize_to_fill_gif: [298, 148]
end
version :size_300x90 do
process crop: INDEX_BY_SIZE[:'300x90'], if: :crop_needed?
process resize_to_fill_gif: [298, 88]
end
version :size_250x90 do
process crop: INDEX_BY_SIZE[:'250x90'], if: :crop_needed?
process resize_to_fill_gif: [248, 88]
end
def crop_needed?(_image)
model.x.present? && model.y.present? && model.width.present? && model.height.present?
end
def filename
"#{secure_token}.#{file.extension}" if original_filename.present?
end
def crop(index = 0)
x = model.x[index].to_i
y = model.y[index].to_i
w = model.width[index].to_i
h = model.height[index].to_i
manipulate! do |img|
if img[:format].downcase == 'gif'
img.repage("0x0")
img.coalesce
img.crop("#{w}x#{h}+#{x}+#{y}")
img << "+repage"
else
img.crop("#{w}x#{h}+#{x}+#{y}")
end
img
end
end
def resize_to_fill_gif(width, height)
if self.file.content_type.include?('gif')
manipulate! do |img|
img_path = img.path
system("gifsicle #{img_path} --resize #{width}x#{height} > /tmp/temp.gif")
system("mv /tmp/temp.gif #{img_path}")
img
end
else
resize_to_fill(width, height)
end
end
protected
def secure_token
var = :"@#{mounted_as}_secure_token"
model.instance_variable_get(var) || model.instance_variable_set(var, SecureRandom.uuid)
end
end