-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from curationexperts/sampling_factor
Set Jpeg sampling factor and strip EXIF tags
- Loading branch information
Showing
2 changed files
with
80 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
module Riiif | ||
# Builds a command to run a transformation using Imagemagick | ||
class ImagemagickCommandFactory | ||
# A helper method to instantiate and invoke build | ||
# @param [String] path the location of the file | ||
# @param [Transformation] transformation | ||
# @param [Integer] compression (85) the compression level to use (set 0 for no compression) | ||
# @param [String] sampling_factor ("4:2:0") the chroma sample factor (set 0 for no compression) | ||
# @param [Boolean] strip_metadata (true) do we want to strip EXIF tags? | ||
# @return [String] a command for running imagemagick to produce the requested output | ||
def self.build(path, transformation, compression: 85) | ||
new(path, transformation, compression: compression).build | ||
def self.build(path, transformation, compression: 85, sampling_factor: '4:2:0', strip_metadata: true) | ||
new(path, transformation, | ||
compression: compression, | ||
sampling_factor: sampling_factor, | ||
strip_metadata: strip_metadata).build | ||
end | ||
|
||
# A helper method to instantiate and invoke build | ||
# @param [String] path the location of the file | ||
# @param [Transformation] transformation | ||
# @param [Integer] compression the compression level to use (set 0 for no compression) | ||
def initialize(path, transformation, compression:) | ||
def initialize(path, transformation, compression:, sampling_factor:, strip_metadata:) | ||
@path = path | ||
@transformation = transformation | ||
@compression = compression | ||
@sampling_factor = sampling_factor | ||
@strip_metadata = strip_metadata | ||
end | ||
|
||
attr_reader :path, :transformation, :compression | ||
attr_reader :path, :transformation, :compression, :sampling_factor, :strip_metadata | ||
|
||
# @return [String] a command for running imagemagick to produce the requested output | ||
def build | ||
command = 'convert' | ||
command << " -crop #{transformation.crop}" if transformation.crop | ||
command << " -resize #{transformation.size}" if transformation.size | ||
if transformation.rotation | ||
command << " -virtual-pixel white +distort srt #{transformation.rotation}" | ||
end | ||
|
||
case transformation.quality | ||
when 'grey' | ||
command << ' -colorspace Gray' | ||
when 'bitonal' | ||
command << ' -colorspace Gray' | ||
command << ' -type Bilevel' | ||
end | ||
command << " -quality #{compression}" if use_compression? | ||
command << " #{path} #{transformation.format}:-" | ||
command | ||
[command, crop, size, rotation, colorspace, quality, sampling, metadata, output].join | ||
end | ||
|
||
private | ||
|
||
def command | ||
'convert' | ||
end | ||
|
||
def use_compression? | ||
compression > 0 && transformation.format == 'jpg' | ||
compression > 0 && jpeg? | ||
end | ||
|
||
def jpeg? | ||
transformation.format == 'jpg'.freeze | ||
end | ||
|
||
def output | ||
" #{path} #{transformation.format}:-" | ||
end | ||
|
||
def crop | ||
" -crop #{transformation.crop}" if transformation.crop | ||
end | ||
|
||
def size | ||
" -resize #{transformation.size}" if transformation.size | ||
end | ||
|
||
def rotation | ||
" -virtual-pixel white +distort srt #{transformation.rotation}" if transformation.rotation | ||
end | ||
|
||
def quality | ||
" -quality #{compression}" if use_compression? | ||
end | ||
|
||
def metadata | ||
' -strip' if strip_metadata | ||
end | ||
|
||
def sampling | ||
" -sampling-factor #{sampling_factor}" if jpeg? | ||
end | ||
|
||
def colorspace | ||
case transformation.quality | ||
when 'grey' | ||
' -colorspace Gray' | ||
when 'bitonal' | ||
' -colorspace Gray -type Bilevel' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters