summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--picture.go29
-rw-r--r--picture_test.go13
-rw-r--r--sheet.go2
-rw-r--r--test/images/excel.emfbin0 -> 12020 bytes
-rw-r--r--xmlDrawing.go3
5 files changed, 27 insertions, 20 deletions
diff --git a/picture.go b/picture.go
index 5e8f6b8..f8133ca 100644
--- a/picture.go
+++ b/picture.go
@@ -113,7 +113,7 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
if _, err = os.Stat(picture); os.IsNotExist(err) {
return err
}
- ext, ok := supportImageTypes[path.Ext(picture)]
+ ext, ok := supportedImageTypes[path.Ext(picture)]
if !ok {
return ErrImgExt
}
@@ -154,7 +154,7 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
func (f *File) AddPictureFromBytes(sheet, cell, format, name, extension string, file []byte) error {
var drawingHyperlinkRID int
var hyperlinkType string
- ext, ok := supportImageTypes[extension]
+ ext, ok := supportedImageTypes[extension]
if !ok {
return ErrImgExt
}
@@ -366,23 +366,20 @@ func (f *File) addMedia(file []byte, ext string) string {
// setContentTypePartImageExtensions provides a function to set the content
// type for relationship parts and the Main Document part.
func (f *File) setContentTypePartImageExtensions() {
- imageTypes := map[string]bool{"jpeg": false, "png": false, "gif": false, "tiff": false}
+ imageTypes := map[string]string{"jpeg": "image/", "png": "image/", "gif": "image/", "tiff": "image/", "emf": "image/x-"}
content := f.contentTypesReader()
content.Lock()
defer content.Unlock()
- for _, v := range content.Defaults {
- _, ok := imageTypes[v.Extension]
- if ok {
- imageTypes[v.Extension] = true
+ for _, file := range content.Defaults {
+ if _, ok := imageTypes[file.Extension]; ok {
+ delete(imageTypes, file.Extension)
}
}
- for k, v := range imageTypes {
- if !v {
- content.Defaults = append(content.Defaults, xlsxDefault{
- Extension: k,
- ContentType: "image/" + k,
- })
- }
+ for extension, prefix := range imageTypes {
+ content.Defaults = append(content.Defaults, xlsxDefault{
+ Extension: extension,
+ ContentType: prefix + extension,
+ })
}
}
@@ -576,7 +573,7 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string)
if err = nil; deTwoCellAnchor.From != nil && deTwoCellAnchor.Pic != nil {
if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
drawRel = f.getDrawingRelationships(drawingRelationships, deTwoCellAnchor.Pic.BlipFill.Blip.Embed)
- if _, ok = supportImageTypes[filepath.Ext(drawRel.Target)]; ok {
+ if _, ok = supportedImageTypes[filepath.Ext(drawRel.Target)]; ok {
ret = filepath.Base(drawRel.Target)
if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil {
buf = buffer.([]byte)
@@ -605,7 +602,7 @@ func (f *File) getPictureFromWsDr(row, col int, drawingRelationships string, wsD
if anchor.From.Col == col && anchor.From.Row == row {
if drawRel = f.getDrawingRelationships(drawingRelationships,
anchor.Pic.BlipFill.Blip.Embed); drawRel != nil {
- if _, ok = supportImageTypes[filepath.Ext(drawRel.Target)]; ok {
+ if _, ok = supportedImageTypes[filepath.Ext(drawRel.Target)]; ok {
ret = filepath.Base(drawRel.Target)
if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil {
buf = buffer.([]byte)
diff --git a/picture_test.go b/picture_test.go
index fbbdf11..c548035 100644
--- a/picture_test.go
+++ b/picture_test.go
@@ -2,9 +2,11 @@ package excelize
import (
"fmt"
+ "image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
+ "io"
"io/ioutil"
"os"
"path/filepath"
@@ -66,7 +68,7 @@ func TestAddPicture(t *testing.T) {
assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
// Test write file to given path.
- assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
+ assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture1.xlsx")))
assert.NoError(t, f.Close())
}
@@ -89,7 +91,14 @@ func TestAddPictureErrors(t *testing.T) {
// Test add picture to worksheet with invalid file data.
err = f.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", ".jpg", make([]byte, 1))
- assert.EqualError(t, err, "image: unknown format")
+ assert.EqualError(t, err, image.ErrFormat.Error())
+
+ // Test add picture with custom image decoder and encoder.
+ decode := func(r io.Reader) (image.Image, error) { return nil, nil }
+ decodeConfig := func(r io.Reader) (image.Config, error) { return image.Config{Height: 100, Width: 90}, nil }
+ image.RegisterFormat("emf", "", decode, decodeConfig)
+ assert.NoError(t, f.AddPicture("Sheet1", "Q1", filepath.Join("test", "images", "excel.emf"), ""))
+ assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture2.xlsx")))
assert.NoError(t, f.Close())
}
diff --git a/sheet.go b/sheet.go
index 3986cd8..4665fd9 100644
--- a/sheet.go
+++ b/sheet.go
@@ -463,7 +463,7 @@ func (f *File) SetSheetBackground(sheet, picture string) error {
if _, err = os.Stat(picture); os.IsNotExist(err) {
return err
}
- ext, ok := supportImageTypes[path.Ext(picture)]
+ ext, ok := supportedImageTypes[path.Ext(picture)]
if !ok {
return ErrImgExt
}
diff --git a/test/images/excel.emf b/test/images/excel.emf
new file mode 100644
index 0000000..9daa6de
--- /dev/null
+++ b/test/images/excel.emf
Binary files differ
diff --git a/xmlDrawing.go b/xmlDrawing.go
index d6d6135..7d0b2df 100644
--- a/xmlDrawing.go
+++ b/xmlDrawing.go
@@ -118,7 +118,8 @@ const (
pivotTableVersion = 3
)
-var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png", ".tif": ".tiff", ".tiff": ".tiff"}
+// supportedImageTypes defined supported image types.
+var supportedImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png", ".tif": ".tiff", ".tiff": ".tiff", ".emf": ".emf"}
// xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This
// element specifies non-visual canvas properties. This allows for additional