mirror of
https://github.com/opf/openproject.git
synced 2026-06-14 03:30:14 +00:00
[#70333] Support WebP images in PDF exports
https://community.openproject.org/work_packages/70333
This commit is contained in:
@@ -42,7 +42,7 @@ module Exports::PDF::Common::Attachments
|
||||
end
|
||||
|
||||
def pdf_embeddable?(content_type)
|
||||
%w[image/jpeg image/png image/gif].include?(content_type)
|
||||
%w[image/jpeg image/png image/gif image/webp].include?(content_type)
|
||||
end
|
||||
|
||||
def delete_all_resized_images
|
||||
@@ -67,6 +67,7 @@ module Exports::PDF::Common::Attachments
|
||||
|
||||
filename = local_file.path
|
||||
filename = convert_gif_to_png(filename) if attachment.content_type == "image/gif"
|
||||
filename = convert_webp_to_png(filename) if attachment.content_type == "image/webp"
|
||||
|
||||
resize_image(filename)
|
||||
end
|
||||
@@ -86,6 +87,15 @@ module Exports::PDF::Common::Attachments
|
||||
tmp_file
|
||||
end
|
||||
|
||||
def convert_webp_to_png(filename)
|
||||
tmp_file = temp_image_file(".png")
|
||||
|
||||
image = MiniMagick::Image.open(filename)
|
||||
image.format("png")
|
||||
image.write(tmp_file)
|
||||
tmp_file
|
||||
end
|
||||
|
||||
def attachment_by_api_content_src(src)
|
||||
return nil if src.empty?
|
||||
|
||||
|
||||
Vendored
BIN
Binary file not shown.
|
After Width: | Height: | Size: 68 B |
@@ -376,6 +376,28 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageToPdf do
|
||||
expect(exporter.send(:pdf_embeddable?, "image/png")).to be true
|
||||
expect(exporter.send(:pdf_embeddable?, "image/jpeg")).to be true
|
||||
expect(exporter.send(:pdf_embeddable?, "image/gif")).to be true
|
||||
expect(exporter.send(:pdf_embeddable?, "image/webp")).to be true
|
||||
end
|
||||
end
|
||||
|
||||
describe "with WebP image attachment" do
|
||||
let(:webp_path) { Rails.root.join("spec/fixtures/files/image.webp") }
|
||||
let(:webp_attachment) { Attachment.new author: user, file: File.open(webp_path) }
|
||||
let(:attachments) { [webp_attachment] }
|
||||
let(:description) do
|
||||
<<~DESCRIPTION
|
||||
This work package contains a WebP image.
|
||||

|
||||
DESCRIPTION
|
||||
end
|
||||
|
||||
before do
|
||||
webp_attachment.save
|
||||
end
|
||||
|
||||
it "converts WebP images and includes them in the PDF export" do
|
||||
expect(webp_attachment.content_type).to eq "image/webp"
|
||||
expect(pdf[:images].length).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user