crop.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package processing
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/imagedata"
  4. "github.com/imgproxy/imgproxy/v3/imath"
  5. "github.com/imgproxy/imgproxy/v3/options"
  6. "github.com/imgproxy/imgproxy/v3/vips"
  7. )
  8. func cropImage(img *vips.Image, cropWidth, cropHeight int, gravity *options.GravityOptions, offsetScale float64) error {
  9. if cropWidth == 0 && cropHeight == 0 {
  10. return nil
  11. }
  12. imgWidth, imgHeight := img.Width(), img.Height()
  13. cropWidth = imath.MinNonZero(cropWidth, imgWidth)
  14. cropHeight = imath.MinNonZero(cropHeight, imgHeight)
  15. if cropWidth >= imgWidth && cropHeight >= imgHeight {
  16. return nil
  17. }
  18. if gravity.Type == options.GravitySmart {
  19. if err := img.CopyMemory(); err != nil {
  20. return err
  21. }
  22. return img.SmartCrop(cropWidth, cropHeight)
  23. }
  24. left, top := calcPosition(imgWidth, imgHeight, cropWidth, cropHeight, gravity, offsetScale, false)
  25. return img.Crop(left, top, cropWidth, cropHeight)
  26. }
  27. func crop(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  28. width, height := pctx.cropWidth, pctx.cropHeight
  29. opts := pctx.cropGravity
  30. opts.RotateAndFlip(pctx.angle, pctx.flip)
  31. opts.RotateAndFlip(po.Rotate, false)
  32. if (pctx.angle+po.Rotate)%180 == 90 {
  33. width, height = height, width
  34. }
  35. // Since we crop before scaling, we shouldn't consider DPR
  36. return cropImage(img, width, height, &opts, 1.0)
  37. }
  38. func cropToResult(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  39. // Crop image to the result size
  40. resultWidth, resultHeight := resultSize(po, pctx.dprScale)
  41. if po.ResizingType == options.ResizeFillDown {
  42. diffW := float64(resultWidth) / float64(img.Width())
  43. diffH := float64(resultHeight) / float64(img.Height())
  44. switch {
  45. case diffW > diffH && diffW > 1.0:
  46. resultHeight = imath.Scale(img.Width(), float64(resultHeight)/float64(resultWidth))
  47. resultWidth = img.Width()
  48. case diffH > diffW && diffH > 1.0:
  49. resultWidth = imath.Scale(img.Height(), float64(resultWidth)/float64(resultHeight))
  50. resultHeight = img.Height()
  51. }
  52. }
  53. return cropImage(img, resultWidth, resultHeight, &po.Gravity, pctx.dprScale)
  54. }