crop.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package processing
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/imath"
  4. "github.com/imgproxy/imgproxy/v3/options"
  5. "github.com/imgproxy/imgproxy/v3/vips"
  6. )
  7. func cropImage(img *vips.Image, cropWidth, cropHeight int, gravity *GravityOptions, offsetScale float64) error {
  8. if cropWidth == 0 && cropHeight == 0 {
  9. return nil
  10. }
  11. imgWidth, imgHeight := img.Width(), img.Height()
  12. cropWidth = imath.MinNonZero(cropWidth, imgWidth)
  13. cropHeight = imath.MinNonZero(cropHeight, imgHeight)
  14. if cropWidth >= imgWidth && cropHeight >= imgHeight {
  15. return nil
  16. }
  17. if gravity.Type == options.GravitySmart {
  18. if err := img.CopyMemory(); err != nil {
  19. return err
  20. }
  21. return img.SmartCrop(cropWidth, cropHeight)
  22. }
  23. left, top := calcPosition(imgWidth, imgHeight, cropWidth, cropHeight, gravity, offsetScale, false)
  24. return img.Crop(left, top, cropWidth, cropHeight)
  25. }
  26. func (p *Processor) crop(c *Context) error {
  27. width, height := c.CropWidth, c.CropHeight
  28. rotateAngle := c.PO.Rotate()
  29. opts := c.CropGravity
  30. opts.RotateAndFlip(c.Angle, c.Flip)
  31. opts.RotateAndFlip(rotateAngle, false)
  32. if (c.Angle+rotateAngle)%180 == 90 {
  33. width, height = height, width
  34. }
  35. // Since we crop before scaling, we shouldn't consider DPR
  36. return cropImage(c.Img, width, height, &opts, 1.0)
  37. }
  38. func (p *Processor) cropToResult(c *Context) error {
  39. gravity := c.PO.Gravity()
  40. return cropImage(c.Img, c.ResultCropWidth, c.ResultCropHeight, &gravity, c.DprScale)
  41. }