Skip to content

Commit

Permalink
Add support for WebP
Browse files Browse the repository at this point in the history
  • Loading branch information
elektronaut committed Mar 13, 2020
1 parent 4c80691 commit ef73e92
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,4 @@ DEPENDENCIES
sqlite3

BUNDLED WITH
1.17.2
2.1.2
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ uploaded, DynamicImage stores the original file and generates images
on demand. It handles cropping, resizing, format and colorspace
conversion.

Supported formats at the moment are JPEG, PNG, GIF, BMP and TIFF. The
latter will automatically be converted to JPG. CMYK images will be
converted to RGB, and RGB images will be converted to the sRGB
Supported formats at the moment are JPEG, PNG, GIF, BMP, WebP and TIFF.
BMP, WebP and TIFF images will automatically be converted to JPG. CMYK
images will be converted to RGB, and RGB images will be converted to the sRGB
colorspace for consistent appearance in all browsers.

DynamicImage is built on [Dis](https://github.com/elektronaut/dis)
Expand Down
4 changes: 2 additions & 2 deletions lib/dynamic_image/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def render_image(options)
render(file: File.join(File.dirname(__FILE__), "templates/show"),
layout: false, locals: { options: options })
end
format.any(:gif, :jpeg, :png, :tiff) do
format.any(:gif, :jpeg, :png, :tiff, :webp) do
send_image(DynamicImage::ProcessedImage.new(@record, options))
end
end
Expand All @@ -69,7 +69,7 @@ def render_raw_image(disposition: "inline", filename: nil)
return unless stale?(@record)

respond_to do |format|
format.any(:gif, :jpeg, :png, :tiff) do
format.any(:gif, :jpeg, :png, :tiff, :webp) do
send_data(@record.data,
filename: filename,
content_type: @record.content_type,
Expand Down
3 changes: 2 additions & 1 deletion lib/dynamic_image/image_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def magic_bytes
"\xff\xd8", # JPEG
"\x49\x49\x2a\x00", # TIFF
"\x4d\x4d\x00\x2a",
"\x42\x4d" # BMP
"\x42\x4d", # BMP
"\x52\x49\x46\x46" # WEBP
].map { |s| s.dup.force_encoding("binary") }
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/dynamic_image/model/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def allowed_content_types
image/jpeg
image/pjpeg
image/png
image/tiff]
image/tiff
image/webp]
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/dynamic_image/processed_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ def record_format
"image/gif" => "GIF",
"image/jpeg" => "JPEG",
"image/pjpeg" => "JPEG",
"image/tiff" => "TIFF" }[record.content_type]
"image/tiff" => "TIFF",
"image/webp" => "WEBP" }[record.content_type]
end

def require_valid_image!
Expand Down
17 changes: 17 additions & 0 deletions spec/dynamic_image/images_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@ def digested(action, options = {})
expect(metadata.format).to eq("TIFF")
end
end

context "when format is WEBP" do
before do
get(
:show,
params: digested(:show, id: image.id, size: "100x100", format: :webp)
)
end

it "sets the content type" do
expect(response.media_type).to eq("image/webp")
end

it "returns a TIFF image" do
expect(metadata.format).to eq("WEBP")
end
end
end

describe "GET uncropped" do
Expand Down
13 changes: 13 additions & 0 deletions spec/dynamic_image/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def read_image(filename)
let(:png_image) { source_image.tap { |o| o.format("PNG") } }
let(:tiff_image) { read_image("image.tif") }
let(:bmp_image) { source_image.tap { |o| o.format("BMP") } }
let(:webp_image) { read_image("image.webp") }

let(:rgb_image) { source_image }
let(:cmyk_image) { jpeg_image.tap { |o| o.colorspace("CMYK") } }
Expand Down Expand Up @@ -88,6 +89,12 @@ def read_image(filename)
it { is_expected.to eq("image/bmp") }
end

context "when image is WEBP" do
let(:image) { webp_image }

it { is_expected.to eq("image/webp") }
end

context "with invalid data" do
let(:image_data) { "invalid" }

Expand Down Expand Up @@ -164,6 +171,12 @@ def read_image(filename)
it { is_expected.to eq("BMP") }
end

context "when image is WEBP" do
let(:image) { webp_image }

it { is_expected.to eq("WEBP") }
end

context "with invalid data" do
let(:image_data) { "invalid" }

Expand Down
6 changes: 6 additions & 0 deletions spec/dynamic_image/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@

it { is_expected.to eq("image/jpeg") }
end

context "when image is WEBP" do
let(:image) { Image.new(content_type: "image/webp") }

it { is_expected.to eq("image/jpeg") }
end
end

describe "metadata parsing" do
Expand Down
28 changes: 28 additions & 0 deletions spec/dynamic_image/processed_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def read_image(filename)
let(:png_image) { source_image.tap { |o| o.format("PNG") } }
let(:tiff_image) { read_image("image.tif") }
let(:bmp_image) { source_image.tap { |o| o.format("BMP") } }
let(:webp_image) { read_image("image.webp") }

let(:rgb_image) { source_image }
let(:cmyk_image) { jpeg_image.tap { |o| o.colorspace("CMYK") } }
Expand Down Expand Up @@ -72,6 +73,14 @@ def read_image(filename)

it { is_expected.to eq("image/bmp") }
end

context "when format is WEBP" do
let(:processed) do
described_class.new(record, format: :webp)
end

it { is_expected.to eq("image/webp") }
end
end

describe "#cropped_and_resized" do
Expand Down Expand Up @@ -178,6 +187,14 @@ def read_image(filename)
end
end

context "when image is WEBP" do
let(:image) { webp_image }

it "returns a WEBP" do
expect(content_type).to eq("image/webp")
end
end

context "when converting BMP to JPEG" do
let(:image) { bmp_image }
let(:processed) do
Expand All @@ -189,6 +206,17 @@ def read_image(filename)
end
end

context "when converting WEBP to JPEG" do
let(:image) { webp_image }
let(:processed) do
described_class.new(record, format: :jpeg)
end

it "returns a JPEG" do
expect(content_type).to eq("image/jpeg")
end
end

context "when converting PNG to GIF" do
let(:processed) do
described_class.new(record, format: :gif)
Expand Down
2 changes: 2 additions & 0 deletions spec/internal/config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf

Mime::Type.register "image/webp", :webp
Binary file added spec/support/fixtures/image.webp
Binary file not shown.

0 comments on commit ef73e92

Please sign in to comment.