class Sinatra::AssetPack::Image

An image.

Common usage

i = Image['/app/images/background.png']    # Local file path

i.dimensions     # Tuple for [ width, height ]
i.width
i.height

i.dimensions?    # True if dimensions are available
                 # (e.g., if ImageMagick is installed and working)

Public Class Methods

[](fname) click to toggle source

Looks up an image. This makes each image only have one associated instance forever.

# File lib/sinatra/assetpack/image.rb, line 19
def self.[](fname)
  fname = File.expand_path(fname) || fname

  @cache        ||= Hash.new
  @cache[fname] ||= new fname
end
new(file) click to toggle source
# File lib/sinatra/assetpack/image.rb, line 26
def initialize(file)
  @file = file
end

Public Instance Methods

dimensions() click to toggle source
# File lib/sinatra/assetpack/image.rb, line 30
def dimensions
  return @dimensions  unless @dimensions.nil?

  dim = /(\d+) x (\d+)/.match(`file "#{@file}"`)
  w, h = dim[1,2]

   if w.to_i != 0 && h.to_i != 0
     @dimensions = [w.to_i, h.to_i]
   else
     @dimensions = false
   end

rescue => e
  @dimensions = false
end
dimensions?() click to toggle source
# File lib/sinatra/assetpack/image.rb, line 46
def dimensions?
  !! dimensions
end
height() click to toggle source
# File lib/sinatra/assetpack/image.rb, line 54
def height
  dimensions? && dimensions[1]
end
width() click to toggle source
# File lib/sinatra/assetpack/image.rb, line 50
def width
  dimensions? && dimensions[0]
end