mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-25 13:48:53 +08:00
Add images from bytes for presentation and workbook (#295)
This commit is contained in:
parent
a3c3e9ba5b
commit
c6ab0e98df
27
common/helpers.go
Normal file
27
common/helpers.go
Normal file
@ -0,0 +1,27 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/unidoc/unioffice"
|
||||
"github.com/unidoc/unioffice/zippkg"
|
||||
)
|
||||
|
||||
// AddImageToZip adds an image (either from bytes or from disk) and adds it to the zip file.
|
||||
func AddImageToZip(z *zip.Writer, img ImageRef, imageNum int, dt unioffice.DocType) error {
|
||||
filename := unioffice.AbsoluteImageFilename(dt, imageNum, strings.ToLower(img.Format()))
|
||||
if img.Data() != nil && len(*img.Data()) > 0 {
|
||||
if err := zippkg.AddFileFromBytes(z, filename, *img.Data()); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if img.Path() != "" {
|
||||
if err := zippkg.AddFileFromDisk(z, filename, img.Path()); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("unsupported image source: %+v", img)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -262,17 +262,8 @@ func (d *Document) Save(w io.Writer) error {
|
||||
}
|
||||
|
||||
for i, img := range d.Images {
|
||||
fn := fmt.Sprintf("word/media/image%d.%s", i+1, strings.ToLower(img.Format()))
|
||||
if img.Data() != nil {
|
||||
if err := zippkg.AddFileFromBytes(z, fn, *img.Data()); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if img.Path() != "" {
|
||||
if err := zippkg.AddFileFromDisk(z, fn, img.Path()); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
unioffice.Log("unsupported image source: %+v", img)
|
||||
if err := common.AddImageToZip(z, img, i+1, unioffice.DocTypeDocument); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -193,3 +193,11 @@ func AbsoluteFilename(dt DocType, typ string, index int) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// AbsoluteImageFilename returns the full path to an image from the root of the
|
||||
// zip container.
|
||||
func AbsoluteImageFilename(dt DocType, index int, fileExtension string) string {
|
||||
filename := AbsoluteFilename(dt, ImageType, index)
|
||||
// replace "png" with the actual file extension
|
||||
return filename[0:len(filename)-3] + fileExtension
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/unidoc/unioffice"
|
||||
"github.com/unidoc/unioffice/common"
|
||||
@ -443,14 +442,8 @@ func (p *Presentation) Save(w io.Writer) error {
|
||||
}
|
||||
|
||||
for i, img := range p.Images {
|
||||
fn := unioffice.AbsoluteFilename(unioffice.DocTypePresentation, unioffice.ImageType, i+1)
|
||||
fn = fn[0:len(fn)-3] + strings.ToLower(img.Format())
|
||||
if img.Path() != "" {
|
||||
if err := zippkg.AddFileFromDisk(z, fn, img.Path()); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
unioffice.Log("unsupported image source: %+v", img)
|
||||
if err := common.AddImageToZip(z, img, i+1, unioffice.DocTypePresentation); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -329,13 +329,8 @@ func (wb *Workbook) Save(w io.Writer) error {
|
||||
}
|
||||
|
||||
for i, img := range wb.Images {
|
||||
fn := fmt.Sprintf("xl/media/image%d.%s", i+1, img.Format())
|
||||
if img.Path() != "" {
|
||||
if err := zippkg.AddFileFromDisk(z, fn, img.Path()); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
unioffice.Log("unsupported image source: %+v", img)
|
||||
if err := common.AddImageToZip(z, img, i+1, unioffice.DocTypeSpreadsheet); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user