Skip to content

Commit

Permalink
fix: ensure that VipsImageProcessor#write_to_jpeg uses jpg regardless…
Browse files Browse the repository at this point in the history
… of the output file extension

Since vips chooses format based on the extension, the easiest way to guarantee the expected format is to append .jpg to the filename, then rename
  • Loading branch information
knarewski committed Dec 2, 2024
1 parent f662387 commit 71376be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/morandi/vips_image_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ def write_to_png(_write_to, _orientation = :any)
raise 'not implemented'
end

def write_to_jpeg(write_to, quality = nil)
def write_to_jpeg(target_path, quality = nil)
process!

quality ||= @options.fetch('quality', 97)

@img.write_to_file(write_to, Q: quality)
target_path_jpg = "#{target_path}.jpg" # Vips chooses format based on file extension, this ensures jpg
@img.write_to_file(target_path_jpg, Q: quality)
FileUtils.mv(target_path_jpg, target_path)
end

private
Expand Down
10 changes: 10 additions & 0 deletions spec/morandi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,16 @@
end
end
end

context 'with output file having jpg extension' do
let(:file_out) { "#{super()}.png" }

it 'writes file as jpg' do
process_image

expect(processed_image_type).to eq('jpeg')
end
end
end

context 'pixbuf processor' do
Expand Down

0 comments on commit 71376be

Please sign in to comment.