Sha256: bbf2d4a3862b33ab556cb08002388f9a3112d39f60455352c4d882f964e92eeb

Contents?: true

Size: 1.57 KB

Versions: 11

Compression:

Stored size: 1.57 KB

Contents

module ImageMagickProcessor

  def geometry
    [image_details[:width], image_details[:height]]
  end

  def format
    image_details[:format]
  end

  def image_data
    IO.read(@temp_file)
  end

  def save_as(new_file_path)
    return if new_file_path == @temp_file
    FileUtils.mv(@temp_file, new_file_path)
  end

  def get_new_geometry
    self.class.new(@temp_file).geometry
  end

  def save!
    FileUtils.mv(@temp_file, file)
  end

  def close
    log "Removing temp file: #{@temp_file}"
    FileUtils.rm_f(@temp_file)
  end

  private

  def resize(w, h)
    convert "-resize #{w}x#{h}"
  end

  def resize_and_crop(w, h, gravity)
    gravity = self.class::GRAVITIES[gravity || 'c']
    convert "-resize #{w}x#{h}^^ -gravity #{gravity} -crop #{w}x#{h}+0+0 +repage"
  end

  def crop(width, height, x = 0, y = 0, gravity)
    raise ArgumentError, "you can't give a crop offset and gravity at the same time" if x != 0 and gravity

    x = '+' + x unless x[/^[+-]/]
    y = '+' + y unless y[/^[+-]/]
    args = "-crop #{w}x#{h}#{x}#{y} +repage"

    if gravity and g = self.class::GRAVITIES[gravity]
      args = "-gravity #{gravity} #{args}"
    end

    convert args
  end

  def convert(args)
    @temp_file = "/tmp/#{$$}.#{File.basename(file)}"
    out = `convert "#{file}" #{args} "#{@temp_file}"`
  end

  def image_details
    @image_details ||= identify(file)
  end

  def identify(path)
    out = `identify -ping -format '%m %w %h' "#{path}"`
    format, width, height = out.split
    { :format => format.downcase,
      :width  => width.to_i,
      :height => height.to_i }
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
dimension-0.1.3 lib/dimension/processors/image_magick.rb
dimension-0.1.2 lib/dimension/processors/image_magick.rb
dimension-0.1.1 lib/dimension/processors/image_magick.rb
dimension-0.0.7 lib/dimension/processors/image_magick.rb
dimension-0.0.6 lib/dimension/processors/image_magick.rb
dimension-0.1.0 lib/dimension/processors/image_magick.rb
dimension-0.0.5 lib/dimension/processors/image_magick.rb
dimension-0.0.4 lib/dimension/processors/image_magick.rb
dimension-0.0.3 lib/dimension/processors/image_magick.rb
dimension-0.0.2 lib/dimension/processors/image_magick.rb
dimension-0.0.1 lib/dimension/processors/image_magick.rb