Skip to content
This repository was archived by the owner on Jun 10, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/dimensions/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Reader
GIF_HEADER = [0x47, 0x49, 0x46, 0x38]
PNG_HEADER = [0x89, 0x50, 0x4E, 0x47]
JPEG_HEADER = [0xFF, 0xD8, 0xFF]
PSD_HEADER = [0x38, 0x42, 0x50, 0x53]
TIFF_HEADER_I = [0x49, 0x49, 0x2A, 0x00]
TIFF_HEADER_M = [0x4D, 0x4D, 0x00, 0x2A]

Expand Down Expand Up @@ -45,6 +46,8 @@ def determine_type
@type = :jpeg
elsif match_header(TIFF_HEADER_I, bytes) || match_header(TIFF_HEADER_M, bytes)
@type = :tiff
elsif match_header(PSD_HEADER, bytes)
@type = :psd
end

process @type ? :"extract_#{type}_dimensions" : nil
Expand All @@ -65,6 +68,13 @@ def extract_png_dimensions
end
end

def extract_psd_dimensions
if @size >= 22
@height, @width = @data.unpack("x14N2")
process nil
end
end

def extract_jpeg_dimensions
scanner = JpegScanner.new(@data)
if scanner.scan
Expand Down
Binary file added test/fixtures/f-and-f.psd
Binary file not shown.
4 changes: 4 additions & 0 deletions test/test_dimensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def test_tiff_dimensions
assert_dimensions "short.tif", 50, 20
end

def test_psd_dimensions
assert_dimensions "f-and-f.psd", 193, 147
end

def assert_dimensions(filename, expected_width, expected_height)
actual_width, actual_height = Dimensions.dimensions(fixture_path(filename))
assert_equal "#{expected_width}x#{expected_height}", "#{actual_width}x#{actual_height}"
Expand Down