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) {
|
||||
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)
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
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) {
|
||||
return err
|
||||
}
|
||||
ext, ok := supportImageTypes[path.Ext(picture)]
|
||||
ext, ok := supportedImageTypes[path.Ext(picture)]
|
||||
if !ok {
|
||||
return ErrImgExt
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue