class ZooniverseData::Helpers::Images::Image

Attributes

path[RW]
raise_exceptions[RW]

Public Class Methods

new(path: nil, raise_exceptions: true) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 13
def initialize(path: nil, raise_exceptions: true)
  self.path = path
  self.raise_exceptions = raise_exceptions
end

Public Instance Methods

_run_optimization(command) click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 69
def _run_optimization(command)
  success = system command
  raise ImageOptimizationError.new('Image optimization failed') unless success
  replace_with_tempfile if @tempfile
end
info() click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 27
def info
  @info ||= FastImage.new(path, raise_on_failure: raise_exceptions)
end
optimize() click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 31
def optimize
  tap do
    case type
    when :jpeg
      _run_optimization "jpegtran -copy none -optimize -progressive -outfile '#{ tempfile.path }' '#{ path }'"
    when :png, :bmp, :gif, :tiff
      _run_optimization "optipng #{optipng_options} '#{ path }'"
    end
  end
end
optipng_options() click to toggle source

-strip all only available after 0.7.0 sourceforge.net/p/optipng/bugs/44/

# File lib/zooniverse_data/helpers/images.rb, line 59
def optipng_options
  [ "-o2" , "-quiet" ].tap do |optipng_options|
    if optipng_version = `optipng -v | sed -n 1p`.strip.match(/\s(\d.+):/)
      if optipng_version[1].gsub(".", "").to_i > 70
        optipng_options.unshift("-strip all")
      end
    end
  end.join(" ")
end
remove_tempfile() click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 47
def remove_tempfile
  tempfile.delete
  @tempfile = nil
end
replace_with_tempfile() click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 52
def replace_with_tempfile
  `cp '#{ tempfile.path }' '#{ path }'`
  remove_tempfile
end
size() click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 18
def size
  width, height = info.size
  OpenStruct.new width: width, height: height
end
tempfile() click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 42
def tempfile
  return @tempfile if @tempfile
  @tempfile = Tempfile.new File.basename path
end
type() click to toggle source
# File lib/zooniverse_data/helpers/images.rb, line 23
def type
  info.type
end