Sha256: 8fc4b6b4c176b7682ff96636869bcaae7cab4b7eaa62a89f5d294c7a1ac5ffe4

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require "digest"
require "fileutils"

module Callisto

  class Thumbnail

    attr_accessor :file_path, :args, :flag
    attr_reader   :name, :crop
    attr_writer   :extension, :root_path, :prefix, :public_path, :size, :quality

    def initialize(args = {})
      args.each do |name, value|
        self.send("#{name}=", value)
      end
      self.name = args
    end

    def name=(value)
      token = Digest::MD5.hexdigest(file_path.gsub(root_path.to_s, "") + value.to_s)
      @name = token + extension
    end

    def save
      location = File.join(root_path, prefix)
      if !File.exist?(save_path)
        FileUtils.mkdir_p(location) unless File.directory?(location)
        task = Shell.new("convert", "#{file_path} -strip -quality #{quality || 90} -resize #{size}#{flag} #{crop} #{save_path}")
        pid = Callisto::Pool.instance << task
        Callisto::Pool.instance.wait(pid)
      end
      public_path
    end

    def save_path
      File.join(root_path, prefix, name)
    end

    def public_path
      File.join((@public_path || Callisto.configuration.thumbnail_public_path).to_s, name)
    end

    def crop=(value)
      @crop = "-gravity Center -crop #{size}+0+0 +repage" if value
    end

    def extension
      @extension || Callisto.configuration.thumbnail_extension || File.extname(file_path)
    end

    %w(root_path prefix size quality).each do |attr|
      define_method attr do
        instance_variable_get("@#{attr}") || Callisto.configuration.send("thumbnail_#{attr}")
      end
    end

    def fixed_size=(val)
      self.min_size = val
      self.crop = true
    end

    def min_size=(val)
      self.flag = "^"
      self.size = val
    end

    def max_size=(val)
      self.size = val
      self.flag = "\\>"
      self.crop = true
    end

    def geometry
      task = Shell.new("identify", "-format \"%wx%h\" #{save_path}")
      task.run
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
callisto-0.9 lib/callisto/thumbnail.rb