diff --git a/lib/writeexcel/image.rb b/lib/writeexcel/image.rb index f138ef89..34563377 100644 --- a/lib/writeexcel/image.rb +++ b/lib/writeexcel/image.rb @@ -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] = '' @@ -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. diff --git a/test/helper.rb b/test/helper.rb index 85ba04cc..ce2059c6 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # require 'simplecov' require 'test/unit' - +require 'mimemagic' # SimpleCov.start $LOAD_PATH.unshift(File.dirname(__FILE__)) diff --git a/test/republic_ps.jpg b/test/republic_ps.jpg new file mode 100644 index 00000000..d1e106fe Binary files /dev/null and b/test/republic_ps.jpg differ diff --git a/test/test_image.rb b/test/test_image.rb new file mode 100644 index 00000000..3498815d --- /dev/null +++ b/test/test_image.rb @@ -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 diff --git a/writeexcel.gemspec b/writeexcel.gemspec index f2348c89..ad97b65f 100644 --- a/writeexcel.gemspec +++ b/writeexcel.gemspec @@ -24,4 +24,5 @@ Gem::Specification.new do |gem| "README.rdoc" ] gem.add_development_dependency 'simplecov' + gem.add_development_dependency 'mimemagic' end