Skip to content

Commit

Permalink
ImageMagickCommandFactory: only extract first layer for PDFs
Browse files Browse the repository at this point in the history
  • Loading branch information
dunn committed Dec 14, 2017
1 parent 3584485 commit feba4c4
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 11 deletions.
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(
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

0 comments on commit feba4c4

Please sign in to comment.