Sha256: 0683625b54288279d639d6570a629a22656f43f1009b7d039494b235b0868e84

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 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

2 entries across 2 versions & 1 rubygems

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