Crop images optionally after resizing (#125)

This commit is contained in:
Janos Dobronszki
2021-05-24 15:54:37 +01:00
committed by GitHub
parent 09052d83e1
commit f6cc5af43f
3 changed files with 369 additions and 72 deletions

View File

@@ -149,6 +149,27 @@ func (e *Image) Resize(ctx context.Context, req *img.ResizeRequest, rsp *img.Res
}
resultImage := imaging.Resize(srcImage, int(req.Width), int(req.Height), imaging.Lanczos)
if req.CropOptions != nil {
anchor := imaging.Center
switch req.CropOptions.Anchor {
case "top left":
anchor = imaging.TopLeft
case "top":
anchor = imaging.Top
case "top right":
anchor = imaging.TopRight
case "left":
anchor = imaging.Left
case "bottom left":
anchor = imaging.BottomLeft
case "bottom":
anchor = imaging.Bottom
case "bottom right":
anchor = imaging.BottomRight
}
resultImage = imaging.CropAnchor(resultImage, int(req.Width), int(req.Height),
anchor)
}
buf := new(bytes.Buffer)
switch {