|
|
|
|
@ -1,23 +1,33 @@
|
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
module ImageUtils
|
|
|
|
|
PNG_REGEXP = %r{\Aimage/(?:png|apng|vnd\.mozilla\.apng)\z}
|
|
|
|
|
JPEG_REGEXP = %r{\Aimage/(?:jpeg|jpg|pjpeg)\z}
|
|
|
|
|
ICO_REGEXP = %r{\Aimage/(?:x-icon|vnd\.microsoft\.icon)\z}
|
|
|
|
|
BMP_REGEXP = %r{\Aimage/(?:bmp|x-bmp|x-ms-bmp)\z}
|
|
|
|
|
|
|
|
|
|
UnsupportedFormat = Class.new(StandardError)
|
|
|
|
|
|
|
|
|
|
module_function
|
|
|
|
|
|
|
|
|
|
def load_vips(data, content_type: nil, autorot: false)
|
|
|
|
|
content_type ||= Marcel::MimeType.for(data)
|
|
|
|
|
|
|
|
|
|
if ICO_REGEXP.match?(content_type)
|
|
|
|
|
LoadIco.call(data)
|
|
|
|
|
elsif BMP_REGEXP.match?(content_type)
|
|
|
|
|
LoadBmp.call(data)
|
|
|
|
|
else
|
|
|
|
|
image = Vips::Image.new_from_buffer(data, '')
|
|
|
|
|
|
|
|
|
|
autorot ? image.autorot : image
|
|
|
|
|
end
|
|
|
|
|
image =
|
|
|
|
|
case content_type
|
|
|
|
|
when PNG_REGEXP
|
|
|
|
|
Vips::Image.pngload_buffer(data)
|
|
|
|
|
when JPEG_REGEXP
|
|
|
|
|
Vips::Image.jpegload_buffer(data)
|
|
|
|
|
when ICO_REGEXP
|
|
|
|
|
LoadIco.call(data)
|
|
|
|
|
when BMP_REGEXP
|
|
|
|
|
LoadBmp.call(data)
|
|
|
|
|
else
|
|
|
|
|
raise UnsupportedFormat, content_type.to_s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
autorot ? image.autorot : image
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def blank?(image)
|
|
|
|
|
|