applyTransformations applies all image transformations (crop, resize, flip, filters)
( ctx context.Context, img *vips.Image, p imagorpath.Params, load imagor.LoadFunc, thumbnail, stretch, upscale bool, focalRects []focal, )
| 589 | |
| 590 | // applyTransformations applies all image transformations (crop, resize, flip, filters) |
| 591 | func (v *Processor) applyTransformations( |
| 592 | ctx context.Context, img *vips.Image, p imagorpath.Params, load imagor.LoadFunc, thumbnail, stretch, upscale bool, focalRects []focal, |
| 593 | ) error { |
| 594 | var ( |
| 595 | origWidth = float64(img.Width()) |
| 596 | origHeight = float64(img.PageHeight()) |
| 597 | cropLeft, |
| 598 | cropTop, |
| 599 | cropRight, |
| 600 | cropBottom float64 |
| 601 | ) |
| 602 | if p.CropRight > 0 || p.CropLeft > 0 || p.CropBottom > 0 || p.CropTop > 0 { |
| 603 | cropLeft = math.Max(p.CropLeft, 0) |
| 604 | cropTop = math.Max(p.CropTop, 0) |
| 605 | cropRight = p.CropRight |
| 606 | cropBottom = p.CropBottom |
| 607 | if p.CropLeft < 1 && p.CropTop < 1 && p.CropRight <= 1 && p.CropBottom <= 1 { |
| 608 | cropLeft = math.Round(cropLeft * origWidth) |
| 609 | cropTop = math.Round(cropTop * origHeight) |
| 610 | cropRight = math.Round(cropRight * origWidth) |
| 611 | cropBottom = math.Round(cropBottom * origHeight) |
| 612 | } |
| 613 | if cropRight == 0 { |
| 614 | cropRight = origWidth - 1 |
| 615 | } |
| 616 | if cropBottom == 0 { |
| 617 | cropBottom = origHeight - 1 |
| 618 | } |
| 619 | cropRight = math.Min(cropRight, origWidth-1) |
| 620 | cropBottom = math.Min(cropBottom, origHeight-1) |
| 621 | } |
| 622 | if p.Trim { |
| 623 | if l, t, w, h, err := findTrim(ctx, img, p.TrimBy, p.TrimTolerance); err == nil { |
| 624 | cropLeft = math.Max(cropLeft, float64(l)) |
| 625 | cropTop = math.Max(cropTop, float64(t)) |
| 626 | if cropRight > 0 { |
| 627 | cropRight = math.Min(cropRight, float64(l+w)) |
| 628 | } else { |
| 629 | cropRight = float64(l + w) |
| 630 | } |
| 631 | if cropBottom > 0 { |
| 632 | cropBottom = math.Min(cropBottom, float64(t+h)) |
| 633 | } else { |
| 634 | cropBottom = float64(t + h) |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | if cropRight > cropLeft && cropBottom > cropTop { |
| 639 | if err := img.ExtractAreaMultiPage( |
| 640 | int(cropLeft), int(cropTop), int(cropRight-cropLeft), int(cropBottom-cropTop), |
| 641 | ); err != nil { |
| 642 | return err |
| 643 | } |
| 644 | } |
| 645 | var ( |
| 646 | w = p.Width |
| 647 | h = p.Height |
| 648 | ) |
no test coverage detected