Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image filetype recognition #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
14 changes: 4 additions & 10 deletions lib/writeexcel/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def store_image_record(i, num_images, num_charts, num_filters, num_comments, spi
# Process the image and extract dimensions.
def process
case filetype
when 'PNG'
when 'png'
process_png(@data)
when 'JPG'
when 'jpg', 'jpeg'
process_jpg(@data)
when 'BMP'
when 'bmp'
process_bmp(@data)
# The 14 byte header of the BMP is stripped off.
@data[0, 13] = ''
Expand All @@ -75,13 +75,7 @@ def process
end

def filetype
return 'PNG' if @data.unpack('x A3')[0] == 'PNG'
return 'BMP' if @data.unpack('A2')[0] == 'BM'
if data.unpack('n')[0] == 0xFFD8
return 'JPG' if @data.unpack('x6 A4')[0] == 'JFIF' || @data.unpack('x6 A4')[0] == 'Exif'
else
raise "Unsupported image format for file: #{@filename}\n"
end
MimeMagic.by_magic(@data).subtype
end

# Extract width and height information from a PNG file.
Expand Down
2 changes: 1 addition & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# require 'simplecov'
require 'test/unit'

require 'mimemagic'
# SimpleCov.start

$LOAD_PATH.unshift(File.dirname(__FILE__))
Expand Down
Binary file added test/republic_ps.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions test/test_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
require 'helper'

class TestImage < Test::Unit::TestCase
def setup
@test_file = StringIO.new
@workbook = WriteExcel.new(@test_file)
@worksheet = @workbook.add_worksheet('test', 0)
end

def test_import_image_is_created_by_adobe_photoshop
image = Writeexcel::Image.new(@worksheet, 0, 0, "test/republic_ps.jpg")
image.import
assert_equal(image.height, 120)
assert_equal(image.width, 120)
assert_equal(image.size, 24972)
end
end
1 change: 1 addition & 0 deletions writeexcel.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Gem::Specification.new do |gem|
"README.rdoc"
]
gem.add_development_dependency 'simplecov'
gem.add_development_dependency 'mimemagic'
end