diff --git a/lib/image_utils.rb b/lib/image_utils.rb index c0325736..d3a86958 100644 --- a/lib/image_utils.rb +++ b/lib/image_utils.rb @@ -6,7 +6,7 @@ module ImageUtils module_function - def load_vips(data, content_type: nil) + def load_vips(data, content_type: nil, autorot: false) content_type ||= Marcel::MimeType.for(data) if ICO_REGEXP.match?(content_type) @@ -14,7 +14,9 @@ module ImageUtils elsif BMP_REGEXP.match?(content_type) LoadBmp.call(data) else - Vips::Image.new_from_buffer(data, '') + image = Vips::Image.new_from_buffer(data, '') + + autorot ? image.autorot : image end end diff --git a/lib/submissions/generate_audit_trail.rb b/lib/submissions/generate_audit_trail.rb index 57a9d1c1..2c16f044 100644 --- a/lib/submissions/generate_audit_trail.rb +++ b/lib/submissions/generate_audit_trail.rb @@ -363,7 +363,7 @@ module Submissions image = 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 next unless attachment.content_type.starts_with?('image/') next if attachment.byte_size.zero? @@ -379,7 +379,7 @@ module Submissions if field['type'] == 'image' && !resized_image.has_alpha? StringIO.new(resized_image.colourspace(:srgb).write_to_buffer('.jpg', strip: true)) else - StringIO.new(resized_image.write_to_buffer('.png')) + StringIO.new(resized_image.write_to_buffer('.png', strip: true)) end width = field['type'] == 'initials' ? 50 : 200 diff --git a/lib/submissions/generate_result_attachments.rb b/lib/submissions/generate_result_attachments.rb index 6ca87886..a0b31c11 100644 --- a/lib/submissions/generate_result_attachments.rb +++ b/lib/submissions/generate_result_attachments.rb @@ -313,7 +313,7 @@ module Submissions attachments_data_cache[attachment.uuid] ||= attachment.download ImageUtils.load_vips(attachments_data_cache[attachment.uuid], - content_type: attachment.content_type).autorot + content_type: attachment.content_type, autorot: true) rescue Vips::Error next unless attachment.content_type.starts_with?('image/') next if attachment.byte_size.zero? @@ -358,7 +358,8 @@ module Submissions image_x = area_x + ((half_width - image_width) / 2.0) 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) @@ -425,7 +426,8 @@ module Submissions 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) .draw(canvas, area_x + TEXT_LEFT_MARGIN, @@ -454,7 +456,7 @@ module Submissions attachments_data_cache[attachment.uuid] ||= attachment.download ImageUtils.load_vips(attachments_data_cache[attachment.uuid], - content_type: attachment.content_type).autorot + content_type: attachment.content_type, autorot: true) rescue Vips::Error next unless attachment.content_type.starts_with?('image/') next if attachment.byte_size.zero? @@ -471,7 +473,7 @@ module Submissions if field_type == 'image' && !resized_image.has_alpha? StringIO.new(resized_image.colourspace(:srgb).write_to_buffer('.jpg', strip: true)) else - StringIO.new(resized_image.write_to_buffer('.png')) + StringIO.new(resized_image.write_to_buffer('.png', strip: true)) end canvas.image( diff --git a/lib/submitters/create_stamp_attachment.rb b/lib/submitters/create_stamp_attachment.rb index 9e6700c8..d1e14088 100644 --- a/lib/submitters/create_stamp_attachment.rb +++ b/lib/submitters/create_stamp_attachment.rb @@ -25,7 +25,7 @@ module Submitters def build_attachment(submitter, with_logo: true) 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) diff --git a/lib/templates/process_document.rb b/lib/templates/process_document.rb index 158142ec..6e1963cc 100644 --- a/lib/templates/process_document.rb +++ b/lib/templates/process_document.rb @@ -58,13 +58,13 @@ module Templates def generate_preview_image(attachment, data) 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) 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:, - palette: true, Q: Q, dither: 0)) + palette: true, Q: Q, dither: 0, strip: true)) ActiveStorage::Attachment.create!( blob: ActiveStorage::Blob.create_and_upload!(