Sha256: aff78f0e28d41ad17bd056aa9be5cc726342774b017c024110c7016b7857d578

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

module Sinatra
  module AssetPack
    # 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 Image
      # Looks up an image.
      # This makes each image only have one associated instance forever.
      def self.[](fname)
        fname = File.expand_path(fname) || fname

        @cache        ||= Hash.new
        @cache[fname] ||= new fname
      end

      def initialize(file)
        @file = file
      end

      def dimensions
        return @dimensions  unless @dimensions.nil?

         _, _, dim = `identify "#{@file}"`.split(' ')
         w, h = dim.split('x')

         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

      def dimensions?
        !! dimensions
      end

      def width
        dimensions? && dimensions[0]
      end

      def height
        dimensions? && dimensions[1]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
sinatra-assetpack-flexible-compression-0.0.1 lib/sinatra/assetpack/image.rb
sundawg-sinatra-assetpack-fork-0.0.12.pre1 lib/sinatra/assetpack/image.rb
sinatra-assetpack-0.0.12.pre1 lib/sinatra/assetpack/image.rb
sinatra-assetpack-0.0.11 lib/sinatra/assetpack/image.rb
sinatra-assetpack-0.0.10 lib/sinatra/assetpack/image.rb
sinatra-assetpack-0.0.9 lib/sinatra/assetpack/image.rb
sinatra-assetpack-0.0.8 lib/sinatra/assetpack/image.rb