Skip to content

Commit 89bf83f

Browse files
committed
adjust autorot
1 parent d44b35c commit 89bf83f

5 files changed

Lines changed: 16 additions & 12 deletions

File tree

lib/image_utils.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ module ImageUtils
66

77
module_function
88

9-
def load_vips(data, content_type: nil)
9+
def load_vips(data, content_type: nil, autorot: false)
1010
content_type ||= Marcel::MimeType.for(data)
1111

1212
if ICO_REGEXP.match?(content_type)
1313
LoadIco.call(data)
1414
elsif BMP_REGEXP.match?(content_type)
1515
LoadBmp.call(data)
1616
else
17-
Vips::Image.new_from_buffer(data, '')
17+
image = Vips::Image.new_from_buffer(data, '')
18+
19+
autorot ? image.autorot : image
1820
end
1921
end
2022

lib/submissions/generate_audit_trail.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def build_audit_trail(submission)
363363

364364
image =
365365
begin
366-
ImageUtils.load_vips(attachment.download, content_type: attachment.content_type).autorot
366+
ImageUtils.load_vips(attachment.download, content_type: attachment.content_type, autorot: true)
367367
rescue Vips::Error
368368
next unless attachment.content_type.starts_with?('image/')
369369
next if attachment.byte_size.zero?
@@ -379,7 +379,7 @@ def build_audit_trail(submission)
379379
if field['type'] == 'image' && !resized_image.has_alpha?
380380
StringIO.new(resized_image.colourspace(:srgb).write_to_buffer('.jpg', strip: true))
381381
else
382-
StringIO.new(resized_image.write_to_buffer('.png'))
382+
StringIO.new(resized_image.write_to_buffer('.png', strip: true))
383383
end
384384

385385
width = field['type'] == 'initials' ? 50 : 200

lib/submissions/generate_result_attachments.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def fill_submitter_fields(submitter, account, pdfs_index, with_signature_id:, is
313313
attachments_data_cache[attachment.uuid] ||= attachment.download
314314

315315
ImageUtils.load_vips(attachments_data_cache[attachment.uuid],
316-
content_type: attachment.content_type).autorot
316+
content_type: attachment.content_type, autorot: true)
317317
rescue Vips::Error
318318
next unless attachment.content_type.starts_with?('image/')
319319
next if attachment.byte_size.zero?
@@ -358,7 +358,8 @@ def fill_submitter_fields(submitter, account, pdfs_index, with_signature_id:, is
358358
image_x = area_x + ((half_width - image_width) / 2.0)
359359
image_y = height - area_y - image_height
360360

361-
io = StringIO.new(image.resize([scale * 4, 1].select(&:positive?).min).write_to_buffer('.png'))
361+
io =
362+
StringIO.new(image.resize([scale * 4, 1].select(&:positive?).min).write_to_buffer('.png', strip: true))
362363

363364
canvas.image(io, at: [image_x, image_y], width: image_width, height: image_height)
364365

@@ -425,7 +426,8 @@ def fill_submitter_fields(submitter, account, pdfs_index, with_signature_id:, is
425426

426427
scale = [area_w / image.width, image_height / image.height].min
427428

428-
io = StringIO.new(image.resize([scale * 4, 1].select(&:positive?).min).write_to_buffer('.png'))
429+
io =
430+
StringIO.new(image.resize([scale * 4, 1].select(&:positive?).min).write_to_buffer('.png', strip: true))
429431

430432
layouter.fit([text], area_w, base_font_size / 0.65)
431433
.draw(canvas, area_x + TEXT_LEFT_MARGIN,
@@ -454,7 +456,7 @@ def fill_submitter_fields(submitter, account, pdfs_index, with_signature_id:, is
454456
attachments_data_cache[attachment.uuid] ||= attachment.download
455457

456458
ImageUtils.load_vips(attachments_data_cache[attachment.uuid],
457-
content_type: attachment.content_type).autorot
459+
content_type: attachment.content_type, autorot: true)
458460
rescue Vips::Error
459461
next unless attachment.content_type.starts_with?('image/')
460462
next if attachment.byte_size.zero?
@@ -471,7 +473,7 @@ def fill_submitter_fields(submitter, account, pdfs_index, with_signature_id:, is
471473
if field_type == 'image' && !resized_image.has_alpha?
472474
StringIO.new(resized_image.colourspace(:srgb).write_to_buffer('.jpg', strip: true))
473475
else
474-
StringIO.new(resized_image.write_to_buffer('.png'))
476+
StringIO.new(resized_image.write_to_buffer('.png', strip: true))
475477
end
476478

477479
canvas.image(

lib/submitters/create_stamp_attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def call(submitter, with_logo: true)
2525
def build_attachment(submitter, with_logo: true)
2626
image = generate_stamp_image(submitter, with_logo:)
2727

28-
image_data = image.write_to_buffer('.png')
28+
image_data = image.write_to_buffer('.png', strip: true)
2929

3030
checksum = Digest::MD5.base64digest(image_data)
3131

lib/templates/process_document.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ def process(attachment, data, extract_fields: false)
5858
def generate_preview_image(attachment, data)
5959
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
6060

61-
image = ImageUtils.load_vips(data, content_type: attachment.content_type).autorot
61+
image = ImageUtils.load_vips(data, content_type: attachment.content_type, autorot: true)
6262
image = image.resize(MAX_WIDTH / image.width.to_f)
6363

6464
bitdepth = 2**image.stats.to_a[1..3].pluck(2).uniq.size
6565

6666
io = StringIO.new(image.write_to_buffer(FORMAT, compression: 6, filter: 0, bitdepth:,
67-
palette: true, Q: Q, dither: 0))
67+
palette: true, Q: Q, dither: 0, strip: true))
6868

6969
ActiveStorage::Attachment.create!(
7070
blob: ActiveStorage::Blob.create_and_upload!(

0 commit comments

Comments
 (0)