Files
docuseal/lib/image_utils.rb
T

43 lines
1004 B
Ruby
Raw Normal View History

2025-06-25 22:31:19 +03:00
# frozen_string_literal: true
module ImageUtils
2026-05-21 15:38:16 +03:00
ICO_REGEXP = %r{\Aimage/(?:x-icon|vnd\.microsoft\.icon)\z}
BMP_REGEXP = %r{\Aimage/(?:bmp|x-bmp|x-ms-bmp)\z}
2025-06-25 22:31:19 +03:00
module_function
2026-05-23 09:30:56 +03:00
def load_vips(data, content_type: nil, autorot: false)
2026-05-21 15:38:16 +03:00
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
2026-05-23 09:30:56 +03:00
image = Vips::Image.new_from_buffer(data, '')
autorot ? image.autorot : image
2026-05-21 15:38:16 +03:00
end
end
2025-06-25 22:31:19 +03:00
def blank?(image)
2025-07-02 13:16:47 +03:00
stats = image.stats
min = (0...image.bands).map { |i| stats.getpoint(0, i)[0] }
max = (0...image.bands).map { |i| stats.getpoint(1, i)[0] }
2025-06-25 22:31:19 +03:00
return true if min.all?(255) && max.all?(255)
return true if min.all?(0) && max.all?(0)
false
end
2025-07-29 12:14:26 +03:00
def error?(image)
image = image.crop(0, 0, image.width / 4, 2)
row1, row2 = image.to_a
row1[3..] == row2[..-4] && row1.each_cons(2).none? { |a, b| a == b }
end
2025-06-25 22:31:19 +03:00
end