Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageMagickCommandFactory: only extract first layer for PDFs #113

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,14 @@ Riiif::Image.info_service = lambda do |id, file|
resp = ActiveFedora::SolrService.get("id:#{fs_id}")
doc = resp['response']['docs'].first
raise "Unable to find solr document with id:#{fs_id}" unless doc
{ height: doc['height_is'], width: doc['width_is'] }

# You’ll want default values if you make thumbnails of PDFs or other
# file types that `identify` won’t return dimensions for
{
height: doc["height_is"] || 100,
width: doc["width_is"] || 100,
format: doc["mime_type_ssi"],
}
end

def logger
Expand Down
6 changes: 5 additions & 1 deletion app/models/riiif/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def render(args)
def info
@info ||= begin
result = info_service.call(id, file)
ImageInformation.new(width: result[:width], height: result[:height])
ImageInformation.new(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer we make ImageInformation have a dimensions attribute and add the format attribute. Is there a reason we can't do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think I was confused by the weird override of the initialize method. Updated now though.

width: result[:width],
height: result[:height],
format: result[:format]
)
end
end

Expand Down
9 changes: 6 additions & 3 deletions app/models/riiif/image_information.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ class ImageInformation < IIIF::Image::Dimension

def initialize(*args)
if args.size == 2
Deprecation.warn(self, 'calling initialize without kwargs is deprecated. Use height: and width:')
Deprecation.warn(self, 'calling initialize without kwargs is deprecated. Use named parameters.')
super(width: args.first, height: args.second)
else
super
@width = args.first[:width]
@height = args.first[:height]
@format = args.first[:format]
end
end
attr_reader :format, :height, :width

def to_h
{ width: width, height: height }
{ width: width, height: height, format: format }
end

# Image information is only valid if height and width are present.
Expand Down
11 changes: 9 additions & 2 deletions app/services/riiif/image_magick_info_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ def initialize(path)
end

def extract
height, width = Riiif::CommandRunner.execute("#{external_command} -format %hx%w #{@path}[0]").split('x')
{ height: Integer(height), width: Integer(width) }
height, width, format = Riiif::CommandRunner.execute(
"#{external_command} -format '%h %w %m' #{@path}[0]"
).split(' ')

{
height: Integer(height),
width: Integer(width),
format: format
}
end
end
end
6 changes: 5 additions & 1 deletion app/services/riiif/imagemagick_command_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ def jpeg?
transformation.format == 'jpg'.freeze
end

def layer_spec
'[0]' if info.format =~ /pdf/i
end

def input
" #{path}"
" #{path}#{layer_spec}"
end

# pipe the output to STDOUT
Expand Down
5 changes: 4 additions & 1 deletion spec/controllers/riiif/images_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@

before do
allow(Riiif::Image).to receive(:new).with('abcd1234').and_return(image)
allow(image).to receive(:info).and_return(Riiif::ImageInformation.new(width: 6000, height: 4000))
allow(image).to(
receive(:info).and_return(Riiif::ImageInformation.new(width: 6000, height: 4000, format: 'JPEG'))
)
end

it 'returns info' do
Expand All @@ -148,6 +150,7 @@
'@id' => 'http://test.host/abcd1234',
'width' => 6000,
'height' => 4000,
'format' => 'JPEG',
'profile' => ['http://iiif.io/api/image/2/level1.json', 'formats' => %w(jpg png)],
'protocol' => 'http://iiif.io/api/image'
expect(response.headers['Link']).to eq '<http://iiif.io/api/image/2/level1.json>;rel="profile"'
Expand Down
2 changes: 1 addition & 1 deletion spec/models/riiif/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
describe '#render' do
before do
allow(Riiif::CommandRunner).to receive(:execute)
.with("identify -format %hx%w #{filename}[0]").and_return('131x175')
.with("identify -format '%h %w %m' #{filename}[0]").and_return('131 175 JPEG')
end

describe 'region' do
Expand Down
2 changes: 1 addition & 1 deletion spec/services/riiif/imagemagick_command_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Riiif::ImagemagickCommandFactory do
let(:path) { 'foo.tiff' }
let(:info) { double('foo') }
let(:info) { double(height: 100, width: 100, format: 'JPEG') }

describe '.command' do
subject { instance.command }
Expand Down