class ZooniverseData::Helpers::Images::Converter

Attributes

flags[RW]
input_image[RW]
optimize[RW]
output_image[RW]
raise_exceptions[RW]
remove_original[RW]

Public Class Methods

new(path: nil, raise_exceptions: true, remove_original: true, optimize: true) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 80
def initialize(path: nil, raise_exceptions: true, remove_original: true, optimize: true)
  self.input_image = Image.new path: path, raise_exceptions: raise_exceptions
  self.remove_original = remove_original
  self.optimize = optimize
  self.flags = []
end

Public Instance Methods

adaptive_percentage_resize(percentage) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 116
def adaptive_percentage_resize(percentage)
  percentage_resize percentage, type: 'adaptive'
end
adaptive_resize(width: nil, height: nil, force: true) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 105
def adaptive_resize(width: nil, height: nil, force: true)
  resize width: width, height: height, type: 'adaptive', force: force
end
command(string) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 87
def command(string)
  tap do
    self.flags << string
  end
end
crop(width: nil, height: nil, top: nil, left: nil) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 126
def crop(width: nil, height: nil, top: nil, left: nil)
  tap do
    self.flags << "-crop #{ width }x#{ height }+#{ left }+#{ top } +repage"
  end
end
crop_center(width: nil, height: nil, top: 0, left: 0) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 132
def crop_center(width: nil, height: nil, top: 0, left: 0)
  tap do
    self.flags << "-gravity Center -crop #{ width }x#{ height }+#{ left }+#{ top } +repage"
  end
end
invert()
Alias for: negate
negate() click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 138
def negate
  tap do
    self.flags << "-negate"
  end
end
Also aliased as: invert
percentage_resize(percentage, type: nil) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 109
def percentage_resize(percentage, type: nil)
  tap do
    resize_type = type ? "#{ type }-resize" : 'resize'
    self.flags << "-#{ resize_type } #{ percentage }% +repage"
  end
end
quality(percentage) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 120
def quality(percentage)
  tap do
    self.flags << "-quality #{ percentage }%"
  end
end
resize(width: nil, height: nil, force: true, type: nil) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 93
def resize(width: nil, height: nil, force: true, type: nil)
  tap do
    resize_type = type ? "#{ type }-resize" : 'resize'

    if width && height
      self.flags << "-#{ resize_type } #{ width }x#{ height }#{ force ? '\!' : '' }"
    elsif width || height
      self.flags << "-#{ resize_type } #{ [width, height].join('x') }"
    end
  end
end
to(path: nil, prefix: nil, postfix: nil) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 149
def to(path: nil, prefix: nil, postfix: nil)
  raise ImageConversionError.new('Cannot convert an image without an output path') unless path || prefix || postfix
  tap do
    if prefix || postfix
      input_file = input_image.path
      input_path = File.dirname input_file
      input_ext = File.extname input_file
      input_name = File.basename(input_file).sub input_ext, ''

      input_name = "#{ prefix }_#{ input_name }" if prefix
      input_name = "#{ input_name }_#{ postfix }" if postfix
      path = "#{ input_path }/#{ input_name }#{ input_ext }"
    end

    self.output_image = Image.new path: path, raise_exceptions: raise_exceptions
  end
end
write() click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 167
def write
  raise ImageConversionError.new('Cannot convert an image without an output path') unless output_image

  output_image.tap do
    success = system "convert #{ input_image.path } #{ flags.join(' ') } #{ output_image.path }"
    raise ImageConversionError.new('Image conversion failed') unless success
    `rm -f '#{ input_image.path }'` if remove_original && input_image.path != output_image.path
    output_image.optimize if optimize
  end
end
write_to(path: nil, prefix: nil, postfix: nil) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 145
def write_to(path: nil, prefix: nil, postfix: nil)
  to(path: path, prefix: prefix, postfix: postfix).write
end