Adding support for calculating JPG dimensions
This commit is contained in:
parent
f9d75b3bce
commit
a075a10f59
@ -1,34 +1,99 @@
|
|||||||
module Compass::SassExtensions::Functions::ImageSize
|
module Compass::SassExtensions::Functions::ImageSize
|
||||||
def image_width(image_file)
|
def image_width(image_file)
|
||||||
width = compute_size(image_file).first
|
image_path = real_path(image_file)
|
||||||
|
width = ImageProperties.new(image_path).size.first
|
||||||
Sass::Script::Number.new(width,["px"])
|
Sass::Script::Number.new(width,["px"])
|
||||||
end
|
end
|
||||||
|
|
||||||
def image_height(image_file)
|
def image_height(image_file)
|
||||||
height = compute_size(image_file).last
|
image_path = real_path(image_file)
|
||||||
|
height = ImageProperties.new(image_path).size.last
|
||||||
Sass::Script::Number.new(height, ["px"])
|
Sass::Script::Number.new(height, ["px"])
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
# Returns an array [width,height] containing image dimensions
|
def real_path(image_file)
|
||||||
def compute_size(image_path)
|
path = image_file.value
|
||||||
path = image_path.value
|
|
||||||
# Compute the real path to the image on the file stystem if the images_dir is set.
|
# Compute the real path to the image on the file stystem if the images_dir is set.
|
||||||
real_path = if Compass.configuration.images_dir
|
if Compass.configuration.images_dir
|
||||||
File.join(Compass.configuration.project_path, Compass.configuration.images_dir, path)
|
File.join(Compass.configuration.project_path, Compass.configuration.images_dir, path)
|
||||||
else
|
else
|
||||||
File.join(Compass.configuration.project_path, path)
|
File.join(Compass.configuration.project_path, path)
|
||||||
end
|
end
|
||||||
case real_path
|
end
|
||||||
when /\.png$/i
|
|
||||||
IO.read(real_path)[0x10..0x18].unpack('NN')
|
class ImageProperties
|
||||||
when /\.gif$/i
|
def initialize(file)
|
||||||
IO.read(real_path)[6..10].unpack('SS')
|
@file = file
|
||||||
when /\.jpe?g$/i
|
@file_type = File.extname(@file)[1..-1]
|
||||||
# FIXME jpgs are not straightforward
|
end
|
||||||
raise Compass::Error, "JPEG files are not supported yet."
|
|
||||||
|
def size
|
||||||
|
@dimensions ||= send("get_size_for_#{@file_type}")
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def get_size_for_png
|
||||||
|
IO.read(@file)[0x10..0x18].unpack('NN')
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
get_size_for_jpeg
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_size_for_jpeg
|
||||||
|
jpeg = JPEG.new(@file)
|
||||||
|
[jpeg.width, jpeg.height]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class JPEG
|
||||||
|
attr_reader :width, :height, :bits
|
||||||
|
|
||||||
|
def initialize(file)
|
||||||
|
if file.kind_of? IO
|
||||||
|
examine(file)
|
||||||
else
|
else
|
||||||
raise Compass::Error, "File is either not an image, or is not supported."
|
File.open(file, 'rb') { |io| examine(io) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def examine(io)
|
||||||
|
raise 'malformed JPEG' unless io.getc == 0xFF && io.getc == 0xD8 # SOI
|
||||||
|
|
||||||
|
class << io
|
||||||
|
def readint; (readchar << 8) + readchar; end
|
||||||
|
def readframe; read(readint - 2); end
|
||||||
|
def readsof; [readint, readchar, readint, readint, readchar]; end
|
||||||
|
def next
|
||||||
|
c = readchar while c != 0xFF
|
||||||
|
c = readchar while c == 0xFF
|
||||||
|
c
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
while marker = io.next
|
||||||
|
case marker
|
||||||
|
when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF # SOF markers
|
||||||
|
length, @bits, @height, @width, components = io.readsof
|
||||||
|
raise 'malformed JPEG' unless length == 8 + components * 3
|
||||||
|
when 0xD9, 0xDA: break # EOI, SOS
|
||||||
|
when 0xFE: @comment = io.readframe # COM
|
||||||
|
when 0xE1: io.readframe # APP1, contains EXIF tag
|
||||||
|
else io.readframe # ignore frame
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user