Skip to content

Commit

Permalink
Merge pull request #1083 from sul-dlss/handleImageSizeAndPercentage
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne authored Dec 13, 2023
2 parents 2ad9f22 + 9c2cc6c commit b52b26b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
32 changes: 30 additions & 2 deletions app/models/projection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def image_source
end

def real_transformation
return transformation unless image.is_a? RestrictedImage
return transformation unless (image.is_a? RestrictedImage) || use_original_size?

IIIF::Image::Transformation.new(
region: transformation.region,
Expand All @@ -127,7 +127,7 @@ def restricted_size
size = transformation.size
case size
when IIIF::Image::Size::BestFit
max_size = image.max_size(self)
max_size = max_image_size
if size.width <= max_size.width && size.height <= max_size.height
size
else
Expand All @@ -139,4 +139,32 @@ def restricted_size
size
end
end

# For a full image request, if the requested width and height are larger than the original image,
# this method will return true
# If this is a percentage region, return false.
# Region_dimensions, which is called from image.max_tile_dimensions, will raise an error for percentage regions.
def use_original_size?
return false if transformation.region.is_a? IIIF::Image::Region::Percent

size = transformation.size
max_size = image.max_tile_dimensions.call(self)
(size.is_a? IIIF::Image::Size::BestFit) && max_size.width < size.width && max_size.height < size.height
rescue NotImplementedError, ArgumentError => e
Honeybadger.notify(e, error_message: "Size check error for #{transformation.inspect}") if Rails.env.production?
false
end

# The original restricted image function used max_size, but the StacksImage class does not have that method
# Since we are using the restricted image block for both RestrictedImage and StacksImage, we check
# which method is available
def max_image_size
image.respond_to?(:max_size) ? image.max_size(self) : dimensions_to_size(image.max_tile_dimensions.call(self))
end

# StacksImage has max_tile_dimensions, which returns dimensions
# We need to create a Size object in order to pass back to create the Iiif image object for image_source
def dimensions_to_size(dimensions)
IIIF::Image::Size::BestFit.new(dimensions.width, dimensions.height)
end
end
32 changes: 32 additions & 0 deletions spec/models/projection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@
expect(http_client).to have_received(:get).with(%r{/full/max/0/default.jpg})
end
end

context "best fit size" do
let(:options) { { size: '!850,700', region: 'full' } }

it 'returns original size when requested dimensions are larger' do
allow(HTTP).to receive(:use)
.and_return(http_client)
allow(http_client).to receive(:get).and_return(double(body: nil))
subject.response
expect(http_client).to have_received(:get).with(%r{/full/!800,600/0/default.jpg})
end
end
end

context 'for a restricted image' do
Expand Down Expand Up @@ -318,4 +330,24 @@
it { is_expected.to be false }
end
end

describe '#use_original_size?' do
let(:image) { StacksImage.new id: 'ab123cd4567', file_name: 'b' }
subject(:use_original) { described_class.new(image, transformation).send(:use_original_size?) }

context 'when percentage region is requested' do
let(:options) { { size: 'full', region: 'pct:3.0,3.0,77.0,77.0' } }
it { is_expected.to be false }
end

context 'when dimensions smaller than original size are requested' do
let(:options) { { size: '!460,460', region: 'full' } }
it { is_expected.to be false }
end

context 'when dimensions larger than original size are requested' do
let(:options) { { size: '!900,900', region: 'full' } }
it { is_expected.to be true }
end
end
end

0 comments on commit b52b26b

Please sign in to comment.