Class: Sinatra::AssetPack::Image
- Inherits:
-
Object
- Object
- Sinatra::AssetPack::Image
- Defined in:
- lib/sinatra/assetpack/image.rb
Overview
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)
Class Method Summary collapse
-
.[](fname) ⇒ Object
Looks up an image.
Instance Method Summary collapse
- #dimensions ⇒ Object
- #dimensions? ⇒ Boolean
- #height ⇒ Object
-
#initialize(file) ⇒ Image
constructor
A new instance of Image.
- #width ⇒ Object
Constructor Details
#initialize(file) ⇒ Image
Returns a new instance of Image.
26 27 28 |
# File 'lib/sinatra/assetpack/image.rb', line 26 def initialize(file) @file = file end |
Class Method Details
.[](fname) ⇒ Object
Looks up an image. This makes each image only have one associated instance forever.
19 20 21 22 23 24 |
# File 'lib/sinatra/assetpack/image.rb', line 19 def self.[](fname) fname = File.(fname) || fname @cache ||= Hash.new @cache[fname] ||= new fname end |
Instance Method Details
#dimensions ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# 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? ⇒ Boolean
46 47 48 |
# File 'lib/sinatra/assetpack/image.rb', line 46 def dimensions? !! dimensions end |
#height ⇒ Object
54 55 56 |
# File 'lib/sinatra/assetpack/image.rb', line 54 def height dimensions? && dimensions[1] end |
#width ⇒ Object
50 51 52 |
# File 'lib/sinatra/assetpack/image.rb', line 50 def width dimensions? && dimensions[0] end |