Adding image_height and image_width functions

This commit is contained in:
Deepak Jois 2010-02-17 19:58:25 -08:00
parent 3dd2a90dec
commit f9d75b3bce
2 changed files with 36 additions and 1 deletions

View File

@ -1,7 +1,7 @@
module Compass::SassExtensions::Functions module Compass::SassExtensions::Functions
end end
%w(selectors enumerate urls display inline_image color_stop font_files).each do |func| %w(selectors enumerate urls display inline_image image_size color_stop font_files).each do |func|
require "compass/sass_extensions/functions/#{func}" require "compass/sass_extensions/functions/#{func}"
end end
@ -11,6 +11,7 @@ module Sass::Script::Functions
include Compass::SassExtensions::Functions::Urls include Compass::SassExtensions::Functions::Urls
include Compass::SassExtensions::Functions::Display include Compass::SassExtensions::Functions::Display
include Compass::SassExtensions::Functions::InlineImage include Compass::SassExtensions::Functions::InlineImage
include Compass::SassExtensions::Functions::ImageSize
include Compass::SassExtensions::Functions::ColorStop include Compass::SassExtensions::Functions::ColorStop
include Compass::SassExtensions::Functions::FontFiles include Compass::SassExtensions::Functions::FontFiles
end end

View File

@ -0,0 +1,34 @@
module Compass::SassExtensions::Functions::ImageSize
def image_width(image_file)
width = compute_size(image_file).first
Sass::Script::Number.new(width,["px"])
end
def image_height(image_file)
height = compute_size(image_file).last
Sass::Script::Number.new(height, ["px"])
end
private
# Returns an array [width,height] containing image dimensions
def compute_size(image_path)
path = image_path.value
# Compute the real path to the image on the file stystem if the images_dir is set.
real_path = if Compass.configuration.images_dir
File.join(Compass.configuration.project_path, Compass.configuration.images_dir, path)
else
File.join(Compass.configuration.project_path, path)
end
case real_path
when /\.png$/i
IO.read(real_path)[0x10..0x18].unpack('NN')
when /\.gif$/i
IO.read(real_path)[6..10].unpack('SS')
when /\.jpe?g$/i
# FIXME jpgs are not straightforward
raise Compass::Error, "JPEG files are not supported yet."
else
raise Compass::Error, "File is either not an image, or is not supported."
end
end
end