Skip to content

Commit 15272d0

Browse files
committed
This closes qax-os#1225, allowing insert EMF format images
1 parent 6d186ff commit 15272d0

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

picture.go

+13-16
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
113113
if _, err = os.Stat(picture); os.IsNotExist(err) {
114114
return err
115115
}
116-
ext, ok := supportImageTypes[path.Ext(picture)]
116+
ext, ok := supportedImageTypes[path.Ext(picture)]
117117
if !ok {
118118
return ErrImgExt
119119
}
@@ -154,7 +154,7 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
154154
func (f *File) AddPictureFromBytes(sheet, cell, format, name, extension string, file []byte) error {
155155
var drawingHyperlinkRID int
156156
var hyperlinkType string
157-
ext, ok := supportImageTypes[extension]
157+
ext, ok := supportedImageTypes[extension]
158158
if !ok {
159159
return ErrImgExt
160160
}
@@ -366,23 +366,20 @@ func (f *File) addMedia(file []byte, ext string) string {
366366
// setContentTypePartImageExtensions provides a function to set the content
367367
// type for relationship parts and the Main Document part.
368368
func (f *File) setContentTypePartImageExtensions() {
369-
imageTypes := map[string]bool{"jpeg": false, "png": false, "gif": false, "tiff": false}
369+
imageTypes := map[string]string{"jpeg": "image/", "png": "image/", "gif": "image/", "tiff": "image/", "emf": "image/x-"}
370370
content := f.contentTypesReader()
371371
content.Lock()
372372
defer content.Unlock()
373-
for _, v := range content.Defaults {
374-
_, ok := imageTypes[v.Extension]
375-
if ok {
376-
imageTypes[v.Extension] = true
373+
for _, file := range content.Defaults {
374+
if _, ok := imageTypes[file.Extension]; ok {
375+
delete(imageTypes, file.Extension)
377376
}
378377
}
379-
for k, v := range imageTypes {
380-
if !v {
381-
content.Defaults = append(content.Defaults, xlsxDefault{
382-
Extension: k,
383-
ContentType: "image/" + k,
384-
})
385-
}
378+
for extension, prefix := range imageTypes {
379+
content.Defaults = append(content.Defaults, xlsxDefault{
380+
Extension: extension,
381+
ContentType: prefix + extension,
382+
})
386383
}
387384
}
388385

@@ -576,7 +573,7 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string)
576573
if err = nil; deTwoCellAnchor.From != nil && deTwoCellAnchor.Pic != nil {
577574
if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
578575
drawRel = f.getDrawingRelationships(drawingRelationships, deTwoCellAnchor.Pic.BlipFill.Blip.Embed)
579-
if _, ok = supportImageTypes[filepath.Ext(drawRel.Target)]; ok {
576+
if _, ok = supportedImageTypes[filepath.Ext(drawRel.Target)]; ok {
580577
ret = filepath.Base(drawRel.Target)
581578
if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil {
582579
buf = buffer.([]byte)
@@ -605,7 +602,7 @@ func (f *File) getPictureFromWsDr(row, col int, drawingRelationships string, wsD
605602
if anchor.From.Col == col && anchor.From.Row == row {
606603
if drawRel = f.getDrawingRelationships(drawingRelationships,
607604
anchor.Pic.BlipFill.Blip.Embed); drawRel != nil {
608-
if _, ok = supportImageTypes[filepath.Ext(drawRel.Target)]; ok {
605+
if _, ok = supportedImageTypes[filepath.Ext(drawRel.Target)]; ok {
609606
ret = filepath.Base(drawRel.Target)
610607
if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil {
611608
buf = buffer.([]byte)

picture_test.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package excelize
22

33
import (
44
"fmt"
5+
"image"
56
_ "image/gif"
67
_ "image/jpeg"
78
_ "image/png"
9+
"io"
810
"io/ioutil"
911
"os"
1012
"path/filepath"
@@ -66,7 +68,7 @@ func TestAddPicture(t *testing.T) {
6668
assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
6769

6870
// Test write file to given path.
69-
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture.xlsx")))
71+
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture1.xlsx")))
7072
assert.NoError(t, f.Close())
7173
}
7274

@@ -89,7 +91,14 @@ func TestAddPictureErrors(t *testing.T) {
8991

9092
// Test add picture to worksheet with invalid file data.
9193
err = f.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", ".jpg", make([]byte, 1))
92-
assert.EqualError(t, err, "image: unknown format")
94+
assert.EqualError(t, err, image.ErrFormat.Error())
95+
96+
// Test add picture with custom image decoder and encoder.
97+
decode := func(r io.Reader) (image.Image, error) { return nil, nil }
98+
decodeConfig := func(r io.Reader) (image.Config, error) { return image.Config{Height: 100, Width: 90}, nil }
99+
image.RegisterFormat("emf", "", decode, decodeConfig)
100+
assert.NoError(t, f.AddPicture("Sheet1", "Q1", filepath.Join("test", "images", "excel.emf"), ""))
101+
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture2.xlsx")))
93102
assert.NoError(t, f.Close())
94103
}
95104

sheet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ func (f *File) SetSheetBackground(sheet, picture string) error {
463463
if _, err = os.Stat(picture); os.IsNotExist(err) {
464464
return err
465465
}
466-
ext, ok := supportImageTypes[path.Ext(picture)]
466+
ext, ok := supportedImageTypes[path.Ext(picture)]
467467
if !ok {
468468
return ErrImgExt
469469
}

test/images/excel.emf

11.7 KB
Binary file not shown.

xmlDrawing.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ const (
118118
pivotTableVersion = 3
119119
)
120120

121-
var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png", ".tif": ".tiff", ".tiff": ".tiff"}
121+
// supportedImageTypes defined supported image types.
122+
var supportedImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png", ".tif": ".tiff", ".tiff": ".tiff", ".emf": ".emf"}
122123

123124
// xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This
124125
// element specifies non-visual canvas properties. This allows for additional

0 commit comments

Comments
 (0)