Add tests, fix some bugs in the image_size helpers, dropped support for bmp because it was broken.

This commit is contained in:
Chris Eppstein 2010-02-22 00:12:23 -08:00
parent bae616e217
commit 0a3947d320
9 changed files with 35 additions and 10 deletions

View File

@ -1,10 +1,12 @@
module Compass::SassExtensions::Functions::ImageSize
# Returns the width of the image relative to the images directory
def image_width(image_file)
image_path = real_path(image_file)
width = ImageProperties.new(image_path).size.first
Sass::Script::Number.new(width,["px"])
end
# Returns the height of the image relative to the images directory
def image_height(image_file)
image_path = real_path(image_file)
height = ImageProperties.new(image_path).size.last
@ -15,8 +17,8 @@ private
def real_path(image_file)
path = image_file.value
# Compute the real path to the image on the file stystem if the images_dir is set.
if Compass.configuration.images_dir
File.join(Compass.configuration.project_path, Compass.configuration.images_dir, path)
if Compass.configuration.images_path
File.join(Compass.configuration.images_path, path)
else
File.join(Compass.configuration.project_path, path)
end
@ -29,7 +31,9 @@ private
end
def size
@dimensions ||= send("get_size_for_#{@file_type}")
@dimensions ||= send(:"get_size_for_#{@file_type}")
rescue NoMethodError
raise Sass::SyntaxError, "Unrecognized file type: #{@file_type}"
end
private
@ -39,12 +43,6 @@ private
def get_size_for_gif
size = IO.read(@file)[6..10].unpack('SS')
size.inspect
end
def get_size_for_bmp
d = IO.read(@file)[14..28]
d[0] == 40 ? d[4..-1].unpack('LL') : d[4..8].unpack('SS')
end
def get_size_for_jpg

View File

@ -42,7 +42,7 @@ class CompassTest < Test::Unit::TestCase
each_css_file(proj.css_path) do |css_file|
assert_no_errors css_file, 'compass'
end
assert_renders_correctly :reset, :layout, :utilities, :gradients
assert_renders_correctly :reset, :layout, :utilities, :gradients, :image_size
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -0,0 +1,15 @@
.png {
width: 100px;
height: 150px; }
.jpg {
width: 100px;
height: 150px; }
.jpeg {
width: 100px;
height: 150px; }
.gif {
width: 100px;
height: 150px; }

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.9 KiB

View File

@ -0,0 +1,12 @@
.png
width= image_width("100x150.png")
height= image_height("100x150.png")
.jpg
width= image_width("100x150.jpg")
height= image_height("100x150.jpg")
.jpeg
width= image_width("100x150.jpeg")
height= image_height("100x150.jpeg")
.gif
width= image_width("100x150.gif")
height= image_height("100x150.gif")