Skip to content

Commit

Permalink
This closes #1225, allowing insert EMF format images
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed May 12, 2022
1 parent eed431e commit 0c3fd02
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
29 changes: 13 additions & 16 deletions picture.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
})
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions picture_test.go
Expand Up @@ -2,9 +2,11 @@ package excelize

import (
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -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())
}

Expand All @@ -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())
}

Expand Down
2 changes: 1 addition & 1 deletion sheet.go
Expand Up @@ -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
}
Expand Down
Binary file added test/images/excel.emf
Binary file not shown.
3 changes: 2 additions & 1 deletion xmlDrawing.go
Expand Up @@ -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
Expand Down

0 comments on commit 0c3fd02

Please sign in to comment.