Sha256: 1bf705ca8fb1cd63ccbea16291843304a07c773282e915a42552f66a0b49b993

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

class Image < Attachment
  def set_file(file)
    super(file)
    crop_image
  end

  def crop_image
    sizes = @field.options['sizes'].to_hash.merge('admin_thumb' => '100x100')
    return unless exist?    
    sizes.each do |size_name, size|
      image = MiniMagick::Image.open(path.to_s)
      image.resize("#{size}^")
      image.format('jpeg')
      image.write(resized_image_path(size_name, false).to_s)
    end
  end

  # TODO: shouldn't always be .jpg; have image extension as an option
  def resized_image_path(size, crop_if_required=true)
    return path if size.nil? || size == :original
    sized_path = File.join(@record.site.attachments_directory, relative_directory_path, "#{size}.jpg")
    crop_image unless File.exist?(sized_path) || !crop_if_required
    sized_path
  end

  # TODO: relative path from is quite a complex method; we should optimise the whole path system here somehow
  def relative_resized_image_path(name, crop_if_required=true)
    Pathname.new(resized_image_path(name, crop_if_required)).relative_path_from(Pathname.new(@record.site.attachments_directory))
  end

  def url(size=:original, crop_if_required=true)
    if size == :original
      super()
    else
      Pathname.new('/').join(relative_resized_image_path(size, crop_if_required))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yodel-0.0.1 lib/yodel/models/core/attachments/image.rb