From 5432978c0d9aca02afa2c1b8cd11a395e7fabaf8 Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Sat, 19 Sep 2026 19:33:58 +0300 Subject: [PATCH] adjust load vips --- lib/image_utils.rb | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/image_utils.rb b/lib/image_utils.rb index d3a86958..9532ff63 100644 --- a/lib/image_utils.rb +++ b/lib/image_utils.rb @@ -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)