Skip to content

Commit

Permalink
Merge pull request #255 from samvera/graphicsmagick
Browse files Browse the repository at this point in the history
Graphicsmagick support for image derivatives
  • Loading branch information
stkenny authored Sep 19, 2023
2 parents 2ea30ae + 52f0b14 commit 7389610
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
name: Install dependencies
command: |
sudo apt-get update
sudo apt-get install ghostscript libpng-dev imagemagick ffmpeg libreoffice dcraw
sudo apt-get install ghostscript libpng-dev imagemagick graphicsmagick ffmpeg libreoffice dcraw
- restore_cache:
name: Restore Kakadu Cache
keys:
Expand Down
1 change: 1 addition & 0 deletions lib/hydra/derivatives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module Derivatives
autoload :PersistExternalFileOutputFileService
autoload :TempfileService
autoload :MimeTypeService
autoload :ImageService
end

# Raised if the timout elapses
Expand Down
30 changes: 26 additions & 4 deletions lib/hydra/derivatives/processors/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,38 @@ def process_with_timeout
# When resizing images, it is necessary to flatten any layers, otherwise the background
# may be completely black. This happens especially with PDFs. See #110
def create_resized_image
create_image do |xfrm|
if Hydra::Derivatives::ImageService.processor == :graphicsmagick
create_resized_image_with_graphicsmagick
else
create_resized_image_with_imagemagick
end
end

def create_resized_image_with_graphicsmagick
Hydra::Derivatives::Logger.debug('[ImageProcessor] Using GraphicsMagick image resize method')
create_image do |temp_file|
if size
xfrm.combine_options do |i|
i.flatten
i.resize(size)
# remove layers and resize using convert instead of mogrify
MiniMagick::Tool::Convert.new do |cmd|
cmd << temp_file.path # input
cmd.flatten
cmd.resize(size)
cmd << temp_file.path # output
end
end
end
end

def create_resized_image_with_imagemagick
Hydra::Derivatives::Logger.debug('[ImageProcessor] Using ImageMagick image resize method')
create_image do |temp_file|
if size
temp_file.flatten
temp_file.resize(size)
end
end
end

def create_image
xfrm = selected_layers(load_image_transformer)
yield(xfrm) if block_given?
Expand Down
22 changes: 22 additions & 0 deletions lib/hydra/derivatives/services/image_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
module Hydra::Derivatives
module ImageService
def self.default_processor
:imagemagick
end

def self.processor
case ENV['IMAGE_PROCESSOR']
when 'imagemagick'
Hydra::Derivatives::Logger.debug('[ImageProcessor] Using ImageMagick as image processor')
:imagemagick
when 'graphicsmagick'
Hydra::Derivatives::Logger.debug('[ImageProcessor] Using GraphicsMagick as image processor')
:graphicsmagick
else
Hydra::Derivatives::Logger.debug("[ImageProcessor] The environment variable IMAGE_PROCESSOR should be set to either 'imagemagick' or 'graphicsmagick'. It is currently set to: #{ENV['IMAGE_PROCESSOR']}. Defaulting to using #{default_processor}")
default_processor
end
end
end
end
Loading

0 comments on commit 7389610

Please sign in to comment.