class Workspace::File

Public Instance Methods

audio?() click to toggle source
# File workspace-media.rb, line 26
def audio?
  ["mp3"].include?(extension)
end
crop(x: 0, y: 0, width: nil, height: nil, quality: 85) click to toggle source
# File workspace-media.rb, line 48
def crop(x: 0, y: 0, width: nil, height: nil, quality: 85)
  width = image.columns - x if width.nil?
  height = image.rows - y if height.nil?
  image = Magick::Image.read(to_s).first
  image.crop!(x, y, width, height)
  image.write(to_s) { self.quality = quality }
  self
ensure image&.destroy!
end
fit(width: nil, height: nil, quality: 85) click to toggle source
# File workspace-media.rb, line 36
def fit(width: nil, height: nil, quality: 85)
  image = Magick::Image.read(to_s).first
  width = [image.columns, image.rows].min if width.nil? and height.nil?
  width = image.columns if width.nil?
  height = width if height.nil?
  image.change_geometry!("#{width}x^#{height}") { |cols, rows| image.thumbnail!(cols, rows) }
  image.crop!(0, 0, width, height)
  image.write(to_s) { self.quality = quality }
  self
ensure image&.destroy!
end
gif?() click to toggle source
# File workspace-media.rb, line 14
def gif?
  ["gif"].include?(extension)
end
image?() click to toggle source
# File workspace-media.rb, line 18
def image?
  ["jpg", "jpeg", "gif", "png"].include?(extension)
end
image_size() click to toggle source
# File workspace-media.rb, line 30
def image_size
  image = Magick::Image.read(to_s).first
  { width: image.columns, height: image.rows }
ensure image&.destroy!
end
jpg?() click to toggle source
# File workspace-media.rb, line 6
def jpg?
  ["jpg", "jpeg"].include?(extension)
end
optimize!(image_max_width: nil, quality: 85, convert_jpg: true, optimize: true) click to toggle source
# File workspace-media.rb, line 58
def optimize!(image_max_width: nil, quality: 85, convert_jpg: true, optimize: true)
  return false if !exists? or !image?

  image = Magick::Image.read(to_s).first
  if !image_max_width.nil? and image.columns > image_max_width
    image.change_geometry!("#{image_max_width}>x") { |cols, rows, img| img.resize!(cols, rows) }
  end
  if mimetype == "image/png" and convert_jpg and (!image.alpha? or image.resize(1, 1).pixel_color(0, 0).opacity == 0)
    image.format = "JPG"
    rename("#{basename}.jpg")
  end
  image.write(to_s) { self.quality = quality }
  available = (jpg? and command?("jpegoptim")) or (png? and command?("optipng"))
  Piet.optimize(to_s, verbose: false, quality: quality) if optimize and available
  self
ensure image&.destroy!
end
png?() click to toggle source
# File workspace-media.rb, line 10
def png?
  ["png"].include?(extension)
end
video?() click to toggle source
# File workspace-media.rb, line 22
def video?
  ["mp4"].include?(extension)
end

Private Instance Methods

command?(name) click to toggle source
# File workspace-media.rb, line 78
def command?(name)
  `which #{name}`
  $?.success?
end