Skip to content

Commit

Permalink
Make sure not do upscale
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-nerdgeschoss committed May 28, 2024
1 parent 1766d78 commit bbd0c6b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/shimmer/utils/file_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(blob_id:, resize: nil, width: nil, height: nil)
resize = if height
"#{width}x#{height}>"
else
"#{width}x"
"#{width}x>"
end
end
@resize = resize
Expand Down
42 changes: 42 additions & 0 deletions spec/utils/shimmer/file_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Shimmer::FileProxy do
let(:blob) {
ActiveStorage::Blob.create_and_upload!(
io: File.open(Rails.root.join("spec/fixtures/files/nerdgeschoss.jpg")),
filename: "example",
content_type: "image/jpeg"
)
}

describe "#variant" do
it "returns the blob if no resize requested" do
variant = Shimmer::FileProxy.new(blob_id: blob.id).variant

expect(variant).to eq(blob)
end

context "when requesting different size" do
it "scales the image down" do
variant = Shimmer::FileProxy.new(blob_id: blob.id, width: 100, height: 200).variant

expect(dimensions(variant.processed.image.blob)).to eq({width: 100, height: 35})
end

it "does not scale the image up" do
variant = Shimmer::FileProxy.new(blob_id: blob.id, width: 1000).variant

original_dimensions = dimensions(blob)
new_dimensions = dimensions(variant.processed.image.blob)

expect(new_dimensions).to eq(original_dimensions)
end
end
end

def dimensions(blob)
ActiveStorage::Analyzer::ImageAnalyzer::ImageMagick.new(blob).metadata
end
end

0 comments on commit bbd0c6b

Please sign in to comment.