Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Myagmartseren committed Mar 23, 2024
1 parent 98004ed commit 14eac67
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion gopdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"compress/zlib" // for constants
"fmt"
"image"
"image/jpeg"
"image/png"
"io"
"log"
Expand Down Expand Up @@ -125,6 +126,9 @@ type ImageOptions struct {

extGStateIndexes []int
}
type ImageFromOption struct {
Format string
}

type MaskOptions struct {
ImageOptions
Expand Down Expand Up @@ -732,6 +736,10 @@ func (gp *GoPdf) Image(picPath string, x float64, y float64, rect *Rect) error {
}

func (gp *GoPdf) ImageFrom(img image.Image, x float64, y float64, rect *Rect) error {
return gp.ImageFromWithOption(img, x, y, rect, ImageFromOption{Format: "png"})
}

func (gp *GoPdf) ImageFromWithOption(img image.Image, x float64, y float64, rect *Rect, option ImageFromOption) error {
if img == nil {
return errors.New("Invalid image")
}
Expand All @@ -741,7 +749,14 @@ func (gp *GoPdf) ImageFrom(img image.Image, x float64, y float64, rect *Rect) er
r, w := io.Pipe()
go func() {
bw := bufio.NewWriter(w)
err := png.Encode(bw, img)
var err error
switch option.Format {
case "png":
err = png.Encode(bw, img)
case "jpeg":
err = jpeg.Encode(bw, img, nil)
}

bw.Flush()
if err != nil {
w.CloseWithError(err)
Expand Down

0 comments on commit 14eac67

Please sign in to comment.