/
image_variant_generator.rb
47 lines (36 loc) · 1.25 KB
/
image_variant_generator.rb
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
require 'RMagick'
require 'fileutils'
class ImageVariantGenerator
VARIANT_DELIMITER = '_'
SupportedImageTypes = ['.gif', '.jpg', '.jpeg', '.png', '.bmp']
attr_accessor :content
attr_accessor :content_type
def self.variant_parser
@variant_parser ||= /(.*)\_(#{Transformations.list.join('|')})(#{SupportedImageTypes.join('|')})/i
end
def self.from_url(server, path)
return nil unless path =~ variant_parser
remote_path = "#{$1}#{$3}"
file = Image.new(server, remote_path)
if file.found?
transform_content(file, $2)
file
else
nil
end
end
def initialize(image)
@image = image
end
def self.transform_content(image, variant)
img = Magick::Image.from_blob(image.content).first
transformation = Transformations[variant]
Logger.current.info_with_time "Transforming image to #{variant}" do
raise ArgumentError, "#{variant} is not a known transformation. (#{Transformations.list.join(', ')})" if transformation.nil?
img = transformation.call(img)
raise ArgumentError, "Creating variant #{variant} for #{path} produced an error. Please return a Magick::Image" if img.nil?
image.content = img.to_blob
end
true
end
end