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
|
# File 'app/controllers/mosaico/images_controller.rb', line 27
def show
case params[:method]
when 'placeholder'
placeholder = find_or_create_placeholder(*params[:params].split(','))
send_image(placeholder)
when 'resize', 'cover'
original_image = UploadedImage.find(id_from_params)
width, height = params[:params].split(',')
width = original_image.width if width == 'null'
height = original_image.height if height == 'null'
width = width.to_i
height = height.to_i
image = UploadedImage.where(parent_id: id_from_params, width: width, height: height).first
send_image(image) and return if image
image_path = UploadedImage.backend.retrieve(original_image.file)
resized_image = MiniMagick::Image.open(image_path)
if params[:method] == 'resize'
resized_image.combine_options do |c|
c.resize("#{width}x#{height}")
c.antialias
end
else
resized_image.combine_options do |c|
c.resize("#{width}x#{height}^")
c.gravity(:center)
c.extent("#{width}x#{height}>")
c.antialias
end
end
dest_file = File.basename(resized_image.path)
UploadedImage.backend.store(resized_image.path, as: dest_file)
resized_record = UploadedImage.create!(
file: dest_file,
width: width,
height: height,
filesize: File.size(resized_image.path),
mime_type: MIME::Types.type_for(resized_image.path).first.to_s,
parent_id: original_image.id
)
send_image(resized_record)
else
if id_from_params
image = UploadedImage.find(id_from_params)
send_image(image)
else
render json: { files: UploadedImage.where(parent_id: nil).map(&:to_json) }
end
end
end
|