1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-10-30 23:08:02 +02:00

Crop between scale-on-load and scale

This commit is contained in:
DarthSim
2022-01-17 18:39:59 +06:00
parent 1ab981ef11
commit 7a968d5fed
9 changed files with 200 additions and 78 deletions

View File

@@ -38,10 +38,20 @@ func cropImage(img *vips.Image, cropWidth, cropHeight int, gravity *options.Grav
}
func crop(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
if err := cropImage(img, pctx.cropWidth, pctx.cropHeight, &pctx.cropGravity); err != nil {
return err
width, height := pctx.cropWidth, pctx.cropHeight
opts := pctx.cropGravity
opts.RotateAndFlip(pctx.angle, pctx.flip)
opts.RotateAndFlip(po.Rotate, false)
if (pctx.angle+po.Rotate)%180 == 90 {
width, height = height, width
}
return cropImage(img, width, height, &opts)
}
func cropToResult(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
// Crop image to the result size
resultWidth := imath.Scale(po.Width, po.Dpr)
resultHeight := imath.Scale(po.Height, po.Dpr)

View File

@@ -161,16 +161,5 @@ func prepare(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptio
pctx.wscale, pctx.hscale = calcScale(widthToScale, heightToScale, po, pctx.imgtype)
if pctx.cropWidth > 0 {
pctx.cropWidth = imath.Max(1, imath.Scale(pctx.cropWidth, pctx.wscale))
}
if pctx.cropHeight > 0 {
pctx.cropHeight = imath.Max(1, imath.Scale(pctx.cropHeight, pctx.hscale))
}
if pctx.cropGravity.Type != options.GravityFocusPoint {
pctx.cropGravity.X *= pctx.wscale
pctx.cropGravity.Y *= pctx.hscale
}
return nil
}

View File

@@ -23,9 +23,10 @@ var mainPipeline = pipeline{
prepare,
scaleOnLoad,
importColorProfile,
crop,
scale,
rotateAndFlip,
crop,
cropToResult,
fixWebpSize,
applyFilters,
extend,

View File

@@ -58,15 +58,29 @@ func scaleOnLoad(pctx *pipelineContext, img *vips.Image, po *options.ProcessingO
// Update scales after scale-on-load
newWidth, newHeight, _, _ := extractMeta(img, po.Rotate, po.AutoRotate)
pctx.wscale = float64(pctx.srcWidth) * pctx.wscale / float64(newWidth)
wpreshrink := float64(pctx.srcWidth) / float64(newWidth)
hpreshrink := float64(pctx.srcHeight) / float64(newHeight)
pctx.wscale = wpreshrink * pctx.wscale
if newWidth == imath.Scale(newWidth, pctx.wscale) {
pctx.wscale = 1.0
}
pctx.hscale = float64(pctx.srcHeight) * pctx.hscale / float64(newHeight)
pctx.hscale = hpreshrink * pctx.hscale
if newHeight == imath.Scale(newHeight, pctx.hscale) {
pctx.hscale = 1.0
}
if pctx.cropWidth > 0 {
pctx.cropWidth = imath.Max(1, imath.Shrink(pctx.cropWidth, wpreshrink))
}
if pctx.cropHeight > 0 {
pctx.cropHeight = imath.Max(1, imath.Shrink(pctx.cropHeight, hpreshrink))
}
if pctx.cropGravity.Type != options.GravityFocusPoint {
pctx.cropGravity.X /= wpreshrink
pctx.cropGravity.Y /= hpreshrink
}
return nil
}