class CSSquirt::ImageFile

Constants

MAX_FILE_SIZE
VALID_FILE_MIMETYPES

Public Class Methods

new(file_path) click to toggle source

Public: Intialize a new ImageFile object.

file_path - A String representing the relative path to an image file.

Raises IOError if the file cannot be found. Raises TypeError if the file is not a valid image file. Raises RangeError if the file is larger than the maximum size that can be

encoded as a Data URI (default: 32 kilobytes).
# File lib/cssquirt/image_file.rb, line 16
def initialize(file_path)
  @file_path = file_path
  raise IOError, "No file found at #{file_path}!" unless File.exist? file_path

  unless self.valid_image_format?
    raise TypeError, "File #{file_path} reports type #{self.filetype} which is not a supported image format."
  end

  file_size = File.size(@file_path)
  if file_size > MAX_FILE_SIZE
    raise RangeError, "File #{file_path} is too big - #{file_size} greater than #{MAX_FILE_SIZE}."
  end
end

Public Instance Methods

as_css_background() click to toggle source

Public: Formats a CSS background image rule for the file.

Returns the CSS background rule as a String.

# File lib/cssquirt/image_file.rb, line 66
def as_css_background()
  "background: url(#{self.encode}) no-repeat;"
end
as_css_background_with_class(klass=nil) click to toggle source

Public: Formats a CSS background image rule for the file, and wraps

it in a class definition.

klass - The name of the class to use for the CSS rule. Optional. If

ommitted, the default will the basename of the file path.

Returns the class wrapped CSS background rule as a String.

# File lib/cssquirt/image_file.rb, line 58
def as_css_background_with_class(klass=nil)
  klass=File.basename(@file_path).tr('.','_') unless klass
  ".#{klass} {\n  #{self.as_css_background}\n}"
end
as_img_tag() click to toggle source

Public: Formats a data-uri based HTML IMG tag for the file.

Returns the entire IMG tag as a String.

# File lib/cssquirt/image_file.rb, line 47
def as_img_tag()
  "<img src='#{self.encode}' />"
end
encode() click to toggle source

Public: Encodes file into a CSS string representation.

Returns the Base64 encoded String with filetype information embedded.

# File lib/cssquirt/image_file.rb, line 73
def encode()
  "data:#{self.filetype};base64," + self.raw_encode
end
filetype() click to toggle source

Public: Returns the MIME type for the file.

Examples

ImageFile.new('example.png')
# => 'image/png'
ImageFile.new('example.svg')
# => 'image/svg+xml'

Returns a String representation of the MIME type.

# File lib/cssquirt/image_file.rb, line 40
def filetype
  `file --mime-type -b #{@file_path}`.chomp
end
raw_encode() click to toggle source

Public: file into its raw Base64 string representation.

Returns the Base64 encoded String.

# File lib/cssquirt/image_file.rb, line 80
def raw_encode()
  return Base64.encode64(File.read @file_path).delete("\n") if RUBY_VERSION < "1.9.0"
  Base64.strict_encode64(File.read @file_path)
end

Protected Instance Methods

valid_image_format?() click to toggle source

Protected: is the file of an image format we support?

Returns a Boolean representing image format validity.

# File lib/cssquirt/image_file.rb, line 89
def valid_image_format?
  VALID_FILE_MIMETYPES.include? self.filetype
end