v0.0.458 revert 457 and fix ObjectFitImage
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m49s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m49s
This commit is contained in:
parent
bfa8457e95
commit
d8b2d01274
@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.457"
|
||||
const GoextVersion = "0.0.458"
|
||||
|
||||
const GoextVersionTimestamp = "2024-05-20T00:07:33+0200"
|
||||
const GoextVersionTimestamp = "2024-05-20T00:15:24+0200"
|
||||
|
@ -234,11 +234,15 @@ func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fil
|
||||
|
||||
// we scale the bounding box by fac (both dimension the same amount, to keep the bounding-box ratio)
|
||||
|
||||
// [ow|oh] ==> size of output image (same ratio as bounding box [bbw|bbh])
|
||||
|
||||
ow := int(math.Round(bbw * facOut))
|
||||
oh := int(math.Round(bbh * facOut))
|
||||
|
||||
facScale := mathext.Min(float64(ow)/float64(iw), float64(oh)/float64(ih))
|
||||
|
||||
// [dw|dh] ==> size of destination rect (where to draw source in output image) (same ratio as input image [iw|ih])
|
||||
|
||||
dw := int(math.Round(float64(iw) * facScale))
|
||||
dh := int(math.Round(float64(ih) * facScale))
|
||||
|
||||
@ -248,11 +252,11 @@ func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fil
|
||||
if fit == ImageFitContainCenter {
|
||||
destBounds = image.Rect((ow-dw)/2, (oh-dh)/2, (ow-dw)/2+dw, (oh-dh)/2+dh)
|
||||
} else if fit == ImageFitContainTopLeft {
|
||||
destBounds = image.Rect(0, 0, iw, dh)
|
||||
destBounds = image.Rect(0, 0, dw, dh)
|
||||
} else if fit == ImageFitContainTopRight {
|
||||
destBounds = image.Rect(ow-iw, 0, ow, dh)
|
||||
destBounds = image.Rect(ow-dw, 0, dw, dh)
|
||||
} else if fit == ImageFitContainBottomLeft {
|
||||
destBounds = image.Rect(0, oh-dh, iw, oh)
|
||||
destBounds = image.Rect(0, oh-dh, dw, oh)
|
||||
} else if fit == ImageFitContainBottomRight {
|
||||
destBounds = image.Rect(ow-dw, oh-dh, ow, oh)
|
||||
}
|
||||
|
@ -7,9 +7,7 @@ import (
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type PDFImageRef struct {
|
||||
@ -229,7 +227,7 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
|
||||
reEncodePixelPerMM := 15.0
|
||||
var imageFit *imageext.ImageFit = nil
|
||||
var fillColor color.Color = color.Transparent
|
||||
var compression *imageext.ImageCompresson = nil
|
||||
compression := imageext.CompressionPNGSpeed
|
||||
var crop *imageext.ImageCrop = nil
|
||||
|
||||
for _, opt := range opts {
|
||||
@ -245,14 +243,14 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
|
||||
allowNegativePosition = langext.Coalesce(opt.allowNegativePosition, allowNegativePosition)
|
||||
imageFit = langext.CoalesceOpt(opt.imageFit, imageFit)
|
||||
fillColor = langext.Coalesce(opt.fillColor, fillColor)
|
||||
compression = langext.CoalesceOpt(opt.compression, compression)
|
||||
compression = langext.Coalesce(opt.compression, compression)
|
||||
reEncodePixelPerMM = langext.Coalesce(opt.reEncodePixelPerMM, reEncodePixelPerMM)
|
||||
crop = langext.CoalesceOpt(opt.crop, crop)
|
||||
}
|
||||
|
||||
regName := img.Name
|
||||
|
||||
if imageFit != nil || fillColor != nil || crop != nil || compression != nil {
|
||||
if imageFit != nil || fillColor != nil || crop != nil {
|
||||
|
||||
var dataimg image.Image
|
||||
if img.Image != nil {
|
||||
@ -263,16 +261,6 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
|
||||
b.b.SetError(err)
|
||||
return
|
||||
}
|
||||
img.Image = langext.Ptr(dataimg)
|
||||
}
|
||||
|
||||
_ = os.WriteFile("/tmp/a.png", img.Bin, 0755)
|
||||
|
||||
{
|
||||
bfr := bytes.Buffer{}
|
||||
enc := &png.Encoder{CompressionLevel: png.NoCompression}
|
||||
_ = enc.Encode(&bfr, dataimg)
|
||||
_ = os.WriteFile("/tmp/b.png", img.Bin, 0755)
|
||||
}
|
||||
|
||||
if crop != nil {
|
||||
@ -296,7 +284,7 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
|
||||
}
|
||||
}
|
||||
|
||||
bfr, imgMime, err := imageext.EncodeImage(dataimg, langext.Coalesce(compression, imageext.CompressionPNGSpeed))
|
||||
bfr, imgMime, err := imageext.EncodeImage(dataimg, compression)
|
||||
if err != nil {
|
||||
b.b.SetError(err)
|
||||
return
|
||||
@ -313,8 +301,6 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
|
||||
imageType = "GIF"
|
||||
}
|
||||
|
||||
_ = os.WriteFile("/tmp/c.png", bfr.Bytes(), 0755)
|
||||
|
||||
b.b.RegisterImageOptionsReader(regName, gofpdf.ImageOptions{ImageType: imageType}, &bfr)
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user