diff --git a/lib/compass/sass_extensions/functions.rb b/lib/compass/sass_extensions/functions.rb index c33a3012..a76421bb 100644 --- a/lib/compass/sass_extensions/functions.rb +++ b/lib/compass/sass_extensions/functions.rb @@ -1,7 +1,7 @@ module Compass::SassExtensions::Functions 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}" end @@ -11,6 +11,7 @@ module Sass::Script::Functions include Compass::SassExtensions::Functions::Urls include Compass::SassExtensions::Functions::Display include Compass::SassExtensions::Functions::InlineImage + include Compass::SassExtensions::Functions::ImageSize include Compass::SassExtensions::Functions::ColorStop include Compass::SassExtensions::Functions::FontFiles end diff --git a/lib/compass/sass_extensions/functions/image_size.rb b/lib/compass/sass_extensions/functions/image_size.rb new file mode 100644 index 00000000..addf7a58 --- /dev/null +++ b/lib/compass/sass_extensions/functions/image_size.rb @@ -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