Sha256: 5d44823e8054f0c331454c235e0036c7d98df6e62333066c486a271a396bfbaa

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

require 'RMagick'

class Storage

  def transform(style_value = nil, filename = nil)
    style_value.gsub!(/\s+/, '')

    if style_value.match(/^(\d+)x(\d+)\#$/)
      crop($1.to_i, $2.to_i, filename)
    else
      resize(style_value, filename)
    end
  end

  private
  def resize(style_value = nil, filename = nil)
    new_image = Magick::Image.read(filename).first
    new_image.change_geometry!(style_value) { |cols, rows, img| img.resize!(cols, rows) }
    new_image
  end

  def crop(new_width = nil, new_height = nil, filename = nil)
    new_image = Magick::Image.read(filename).first

    geometry = resize_before_cropping(new_image, new_width, new_height)
    new_image.change_geometry!(geometry) { |cols, rows, img| img.resize!(cols, rows) }

    width = new_image.columns
    height = new_image.rows

    new_image.crop!(width/2 - new_width/2, height/2 - new_height/2, new_width, new_height)
    new_image
  end

  def resize_before_cropping(img = nil, new_width = nil, new_height = nil)
    width = img.columns
    height = img.rows

    if width > height
      "x#{new_height}>"
    elsif (width < height)
      "#{new_width.to_s}x>"
    else
      "#{new_width}x#{new_height}>"
    end
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mm-attach-it-0.2.4 lib/attach_it/storage/storage.rb
mm-attach-it-0.2.3 lib/attach_it/storage/storage.rb
mm-attach-it-0.2.2 lib/attach_it/storage/storage.rb
mm-attach-it-0.2.1 lib/attach_it/storage/storage.rb
mm-attach-it-0.2.0 lib/attach_it/storage/storage.rb
mm-attach-it-0.1.5 lib/attach_it/storage/storage.rb
mm-attach-it-0.1.4 lib/attach_it/storage/storage.rb