Class: CSSquirt::ImageFile
- Inherits:
-
Object
- Object
- CSSquirt::ImageFile
- Defined in:
- lib/cssquirt/image_file.rb
Constant Summary collapse
- MAX_FILE_SIZE =
32768- VALID_FILE_MIMETYPES =
['image/png', 'image/jpeg', 'image/gif', 'image/svg+xml']
Instance Method Summary collapse
-
#as_css_background ⇒ Object
Public: Formats a CSS background image rule for the file.
-
#as_css_background_with_class(klass = nil) ⇒ Object
Public: Formats a CSS background image rule for the file, and wraps it in a class definition.
-
#as_img_tag ⇒ Object
Public: Formats a data-uri based HTML IMG tag for the file.
-
#encode ⇒ Object
Public: Encodes file into a CSS string representation.
-
#filetype ⇒ Object
Public: Returns the MIME type for the file.
-
#initialize(file_path) ⇒ ImageFile
constructor
Public: Intialize a new ImageFile object.
-
#raw_encode ⇒ Object
Public: file into its raw Base64 string representation.
Constructor Details
#initialize(file_path) ⇒ ImageFile
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).
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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 |
Instance Method Details
#as_css_background ⇒ Object
Public: Formats a CSS background image rule for the file.
Returns the CSS background rule as a String.
66 67 68 |
# 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) ⇒ Object
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.
58 59 60 61 |
# 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 ⇒ Object
Public: Formats a data-uri based HTML IMG tag for the file.
Returns the entire IMG tag as a String.
47 48 49 |
# File 'lib/cssquirt/image_file.rb', line 47 def as_img_tag() "<img src='#{self.encode}' />" end |
#encode ⇒ Object
Public: Encodes file into a CSS string representation.
Returns the Base64 encoded String with filetype information embedded.
73 74 75 |
# File 'lib/cssquirt/image_file.rb', line 73 def encode() "data:#{self.filetype};base64," + self.raw_encode end |
#filetype ⇒ Object
40 41 42 |
# File 'lib/cssquirt/image_file.rb', line 40 def filetype `file --mime-type -b #{@file_path}`.chomp end |
#raw_encode ⇒ Object
Public: file into its raw Base64 string representation.
Returns the Base64 encoded String.
80 81 82 83 |
# 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 |