This closes #1225, allowing insert EMF format images
This commit is contained in:
parent
eed431e0fc
commit
0c3fd0223c
29
picture.go
29
picture.go
|
@ -113,7 +113,7 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
|
||||||
if _, err = os.Stat(picture); os.IsNotExist(err) {
|
if _, err = os.Stat(picture); os.IsNotExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ext, ok := supportImageTypes[path.Ext(picture)]
|
ext, ok := supportedImageTypes[path.Ext(picture)]
|
||||||
if !ok {
|
if !ok {
|
||||||
return ErrImgExt
|
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 {
|
func (f *File) AddPictureFromBytes(sheet, cell, format, name, extension string, file []byte) error {
|
||||||
var drawingHyperlinkRID int
|
var drawingHyperlinkRID int
|
||||||
var hyperlinkType string
|
var hyperlinkType string
|
||||||
ext, ok := supportImageTypes[extension]
|
ext, ok := supportedImageTypes[extension]
|
||||||
if !ok {
|
if !ok {
|
||||||
return ErrImgExt
|
return ErrImgExt
|
||||||
}
|
}
|
||||||
|
@ -366,23 +366,20 @@ func (f *File) addMedia(file []byte, ext string) string {
|
||||||
// setContentTypePartImageExtensions provides a function to set the content
|
// setContentTypePartImageExtensions provides a function to set the content
|
||||||
// type for relationship parts and the Main Document part.
|
// type for relationship parts and the Main Document part.
|
||||||
func (f *File) setContentTypePartImageExtensions() {
|
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 := f.contentTypesReader()
|
||||||
content.Lock()
|
content.Lock()
|
||||||
defer content.Unlock()
|
defer content.Unlock()
|
||||||
for _, v := range content.Defaults {
|
for _, file := range content.Defaults {
|
||||||
_, ok := imageTypes[v.Extension]
|
if _, ok := imageTypes[file.Extension]; ok {
|
||||||
if ok {
|
delete(imageTypes, file.Extension)
|
||||||
imageTypes[v.Extension] = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for k, v := range imageTypes {
|
for extension, prefix := range imageTypes {
|
||||||
if !v {
|
content.Defaults = append(content.Defaults, xlsxDefault{
|
||||||
content.Defaults = append(content.Defaults, xlsxDefault{
|
Extension: extension,
|
||||||
Extension: k,
|
ContentType: prefix + extension,
|
||||||
ContentType: "image/" + k,
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -576,7 +573,7 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string)
|
||||||
if err = nil; deTwoCellAnchor.From != nil && deTwoCellAnchor.Pic != nil {
|
if err = nil; deTwoCellAnchor.From != nil && deTwoCellAnchor.Pic != nil {
|
||||||
if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
|
if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
|
||||||
drawRel = f.getDrawingRelationships(drawingRelationships, deTwoCellAnchor.Pic.BlipFill.Blip.Embed)
|
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)
|
ret = filepath.Base(drawRel.Target)
|
||||||
if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil {
|
if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil {
|
||||||
buf = buffer.([]byte)
|
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 anchor.From.Col == col && anchor.From.Row == row {
|
||||||
if drawRel = f.getDrawingRelationships(drawingRelationships,
|
if drawRel = f.getDrawingRelationships(drawingRelationships,
|
||||||
anchor.Pic.BlipFill.Blip.Embed); drawRel != nil {
|
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)
|
ret = filepath.Base(drawRel.Target)
|
||||||
if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil {
|
if buffer, _ := f.Pkg.Load(strings.Replace(drawRel.Target, "..", "xl", -1)); buffer != nil {
|
||||||
buf = buffer.([]byte)
|
buf = buffer.([]byte)
|
||||||
|
|
|
@ -2,9 +2,11 @@ package excelize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -66,7 +68,7 @@ func TestAddPicture(t *testing.T) {
|
||||||
assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
|
assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), ""))
|
||||||
|
|
||||||
// Test write file to given path.
|
// 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())
|
assert.NoError(t, f.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +91,14 @@ func TestAddPictureErrors(t *testing.T) {
|
||||||
|
|
||||||
// Test add picture to worksheet with invalid file data.
|
// Test add picture to worksheet with invalid file data.
|
||||||
err = f.AddPictureFromBytes("Sheet1", "G21", "", "Excel Logo", ".jpg", make([]byte, 1))
|
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())
|
assert.NoError(t, f.Close())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
sheet.go
2
sheet.go
|
@ -463,7 +463,7 @@ func (f *File) SetSheetBackground(sheet, picture string) error {
|
||||||
if _, err = os.Stat(picture); os.IsNotExist(err) {
|
if _, err = os.Stat(picture); os.IsNotExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ext, ok := supportImageTypes[path.Ext(picture)]
|
ext, ok := supportedImageTypes[path.Ext(picture)]
|
||||||
if !ok {
|
if !ok {
|
||||||
return ErrImgExt
|
return ErrImgExt
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
|
@ -118,7 +118,8 @@ const (
|
||||||
pivotTableVersion = 3
|
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
|
// xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This
|
||||||
// element specifies non-visual canvas properties. This allows for additional
|
// element specifies non-visual canvas properties. This allows for additional
|
||||||
|
|
Loading…
Reference in New Issue