adjust autorot

pull/604/merge
Pete Matsyburka 4 weeks ago
parent d44b35cf0a
commit 89bf83febb

@ -6,7 +6,7 @@ module ImageUtils
module_function module_function
def load_vips(data, content_type: nil) def load_vips(data, content_type: nil, autorot: false)
content_type ||= Marcel::MimeType.for(data) content_type ||= Marcel::MimeType.for(data)
if ICO_REGEXP.match?(content_type) if ICO_REGEXP.match?(content_type)
@ -14,7 +14,9 @@ module ImageUtils
elsif BMP_REGEXP.match?(content_type) elsif BMP_REGEXP.match?(content_type)
LoadBmp.call(data) LoadBmp.call(data)
else else
Vips::Image.new_from_buffer(data, '') image = Vips::Image.new_from_buffer(data, '')
autorot ? image.autorot : image
end end
end end

@ -363,7 +363,7 @@ module Submissions
image = image =
begin begin
ImageUtils.load_vips(attachment.download, content_type: attachment.content_type).autorot ImageUtils.load_vips(attachment.download, content_type: attachment.content_type, autorot: true)
rescue Vips::Error rescue Vips::Error
next unless attachment.content_type.starts_with?('image/') next unless attachment.content_type.starts_with?('image/')
next if attachment.byte_size.zero? next if attachment.byte_size.zero?
@ -379,7 +379,7 @@ module Submissions
if field['type'] == 'image' && !resized_image.has_alpha? if field['type'] == 'image' && !resized_image.has_alpha?
StringIO.new(resized_image.colourspace(:srgb).write_to_buffer('.jpg', strip: true)) StringIO.new(resized_image.colourspace(:srgb).write_to_buffer('.jpg', strip: true))
else else
StringIO.new(resized_image.write_to_buffer('.png')) StringIO.new(resized_image.write_to_buffer('.png', strip: true))
end end
width = field['type'] == 'initials' ? 50 : 200 width = field['type'] == 'initials' ? 50 : 200

@ -313,7 +313,7 @@ module Submissions
attachments_data_cache[attachment.uuid] ||= attachment.download attachments_data_cache[attachment.uuid] ||= attachment.download
ImageUtils.load_vips(attachments_data_cache[attachment.uuid], ImageUtils.load_vips(attachments_data_cache[attachment.uuid],
content_type: attachment.content_type).autorot content_type: attachment.content_type, autorot: true)
rescue Vips::Error rescue Vips::Error
next unless attachment.content_type.starts_with?('image/') next unless attachment.content_type.starts_with?('image/')
next if attachment.byte_size.zero? next if attachment.byte_size.zero?
@ -358,7 +358,8 @@ module Submissions
image_x = area_x + ((half_width - image_width) / 2.0) image_x = area_x + ((half_width - image_width) / 2.0)
image_y = height - area_y - image_height image_y = height - area_y - image_height
io = StringIO.new(image.resize([scale * 4, 1].select(&:positive?).min).write_to_buffer('.png')) io =
StringIO.new(image.resize([scale * 4, 1].select(&:positive?).min).write_to_buffer('.png', strip: true))
canvas.image(io, at: [image_x, image_y], width: image_width, height: image_height) canvas.image(io, at: [image_x, image_y], width: image_width, height: image_height)
@ -425,7 +426,8 @@ module Submissions
scale = [area_w / image.width, image_height / image.height].min scale = [area_w / image.width, image_height / image.height].min
io = StringIO.new(image.resize([scale * 4, 1].select(&:positive?).min).write_to_buffer('.png')) io =
StringIO.new(image.resize([scale * 4, 1].select(&:positive?).min).write_to_buffer('.png', strip: true))
layouter.fit([text], area_w, base_font_size / 0.65) layouter.fit([text], area_w, base_font_size / 0.65)
.draw(canvas, area_x + TEXT_LEFT_MARGIN, .draw(canvas, area_x + TEXT_LEFT_MARGIN,
@ -454,7 +456,7 @@ module Submissions
attachments_data_cache[attachment.uuid] ||= attachment.download attachments_data_cache[attachment.uuid] ||= attachment.download
ImageUtils.load_vips(attachments_data_cache[attachment.uuid], ImageUtils.load_vips(attachments_data_cache[attachment.uuid],
content_type: attachment.content_type).autorot content_type: attachment.content_type, autorot: true)
rescue Vips::Error rescue Vips::Error
next unless attachment.content_type.starts_with?('image/') next unless attachment.content_type.starts_with?('image/')
next if attachment.byte_size.zero? next if attachment.byte_size.zero?
@ -471,7 +473,7 @@ module Submissions
if field_type == 'image' && !resized_image.has_alpha? if field_type == 'image' && !resized_image.has_alpha?
StringIO.new(resized_image.colourspace(:srgb).write_to_buffer('.jpg', strip: true)) StringIO.new(resized_image.colourspace(:srgb).write_to_buffer('.jpg', strip: true))
else else
StringIO.new(resized_image.write_to_buffer('.png')) StringIO.new(resized_image.write_to_buffer('.png', strip: true))
end end
canvas.image( canvas.image(

@ -25,7 +25,7 @@ module Submitters
def build_attachment(submitter, with_logo: true) def build_attachment(submitter, with_logo: true)
image = generate_stamp_image(submitter, with_logo:) image = generate_stamp_image(submitter, with_logo:)
image_data = image.write_to_buffer('.png') image_data = image.write_to_buffer('.png', strip: true)
checksum = Digest::MD5.base64digest(image_data) checksum = Digest::MD5.base64digest(image_data)

@ -58,13 +58,13 @@ module Templates
def generate_preview_image(attachment, data) def generate_preview_image(attachment, data)
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
image = ImageUtils.load_vips(data, content_type: attachment.content_type).autorot image = ImageUtils.load_vips(data, content_type: attachment.content_type, autorot: true)
image = image.resize(MAX_WIDTH / image.width.to_f) image = image.resize(MAX_WIDTH / image.width.to_f)
bitdepth = 2**image.stats.to_a[1..3].pluck(2).uniq.size bitdepth = 2**image.stats.to_a[1..3].pluck(2).uniq.size
io = StringIO.new(image.write_to_buffer(FORMAT, compression: 6, filter: 0, bitdepth:, io = StringIO.new(image.write_to_buffer(FORMAT, compression: 6, filter: 0, bitdepth:,
palette: true, Q: Q, dither: 0)) palette: true, Q: Q, dither: 0, strip: true))
ActiveStorage::Attachment.create!( ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!( blob: ActiveStorage::Blob.create_and_upload!(

Loading…
Cancel
Save