1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-29 08:22:11 +02:00
imgproxy/processing/extend.go

47 lines
1.7 KiB
Go

package processing
import (
"github.com/imgproxy/imgproxy/v3/imagedata"
"github.com/imgproxy/imgproxy/v3/imath"
"github.com/imgproxy/imgproxy/v3/options"
"github.com/imgproxy/imgproxy/v3/vips"
)
func extendImage(img *vips.Image, resultWidth, resultHeight int, opts *options.ExtendOptions, offsetScale float64, extendAr bool) error {
if !opts.Enabled || (resultWidth <= img.Width() && resultHeight <= img.Height()) || resultWidth == 0 || resultHeight == 0 {
return nil
}
if extendAr && resultWidth > img.Width() && resultHeight > img.Height() {
diffW := float64(resultWidth) / float64(img.Width())
diffH := float64(resultHeight) / float64(img.Height())
switch {
case diffH > diffW:
resultHeight = imath.Scale(img.Width(), float64(resultHeight)/float64(resultWidth))
resultWidth = img.Width()
case diffW > diffH:
resultWidth = imath.Scale(img.Height(), float64(resultWidth)/float64(resultHeight))
resultHeight = img.Height()
default:
// The image has the requested arpect ratio
return nil
}
}
offX, offY := calcPosition(resultWidth, resultHeight, img.Width(), img.Height(), &opts.Gravity, offsetScale, false)
return img.Embed(resultWidth, resultHeight, offX, offY)
}
func extend(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
resultWidth, resultHeight := resultSize(po, pctx.dprScale)
return extendImage(img, resultWidth, resultHeight, &po.Extend, pctx.dprScale, false)
}
func extendAspectRatio(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
resultWidth, resultHeight := resultSize(po, pctx.dprScale)
return extendImage(img, resultWidth, resultHeight, &po.ExtendAspectRatio, pctx.dprScale, true)
}