mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-06-17 22:37:33 +02:00
Add extend_aspect_ratio processing option
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
## Add
|
||||||
|
- Add [extend_aspect_ratio](https://docs.imgproxy.net/latest/generating_the_url?id=extend-aspect-ratio) processing option.
|
||||||
|
|
||||||
## [3.13.2] - 2023-02-15
|
## [3.13.2] - 2023-02-15
|
||||||
### Change
|
### Change
|
||||||
|
@ -174,6 +174,19 @@ ex:%extend:%gravity
|
|||||||
|
|
||||||
Default: `false:ce:0:0`
|
Default: `false:ce:0:0`
|
||||||
|
|
||||||
|
### Extend aspect ratio
|
||||||
|
|
||||||
|
```
|
||||||
|
extend_aspect_ratio:%extend:%gravity
|
||||||
|
extend_ar:%extend:%gravity
|
||||||
|
exar:%extend:%gravity
|
||||||
|
```
|
||||||
|
|
||||||
|
* When `extend` is set to `1`, `t` or `true`, imgproxy will extend the image to the requested aspect ratio.
|
||||||
|
* `gravity` _(optional)_ accepts the same values as the [gravity](#gravity) option, except `sm`. When `gravity` is not set, imgproxy will use `ce` gravity without offsets.
|
||||||
|
|
||||||
|
Default: `false:ce:0:0`
|
||||||
|
|
||||||
### Gravity
|
### Gravity
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -70,6 +70,7 @@ type ProcessingOptions struct {
|
|||||||
Gravity GravityOptions
|
Gravity GravityOptions
|
||||||
Enlarge bool
|
Enlarge bool
|
||||||
Extend ExtendOptions
|
Extend ExtendOptions
|
||||||
|
ExtendAspectRatio ExtendOptions
|
||||||
Crop CropOptions
|
Crop CropOptions
|
||||||
Padding PaddingOptions
|
Padding PaddingOptions
|
||||||
Trim TrimOptions
|
Trim TrimOptions
|
||||||
@ -120,6 +121,7 @@ func NewProcessingOptions() *ProcessingOptions {
|
|||||||
Gravity: GravityOptions{Type: GravityCenter},
|
Gravity: GravityOptions{Type: GravityCenter},
|
||||||
Enlarge: false,
|
Enlarge: false,
|
||||||
Extend: ExtendOptions{Enabled: false, Gravity: GravityOptions{Type: GravityCenter}},
|
Extend: ExtendOptions{Enabled: false, Gravity: GravityOptions{Type: GravityCenter}},
|
||||||
|
ExtendAspectRatio: ExtendOptions{Enabled: false, Gravity: GravityOptions{Type: GravityCenter}},
|
||||||
Padding: PaddingOptions{Enabled: false},
|
Padding: PaddingOptions{Enabled: false},
|
||||||
Trim: TrimOptions{Enabled: false, Threshold: 10, Smart: true},
|
Trim: TrimOptions{Enabled: false, Threshold: 10, Smart: true},
|
||||||
Rotate: 0,
|
Rotate: 0,
|
||||||
@ -250,6 +252,26 @@ func parseGravity(g *GravityOptions, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseExtend(opts *ExtendOptions, name string, args []string) error {
|
||||||
|
if len(args) > 4 {
|
||||||
|
return fmt.Errorf("Invalid %s arguments: %v", name, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
opts.Enabled = parseBoolOption(args[0])
|
||||||
|
|
||||||
|
if len(args) > 1 {
|
||||||
|
if err := parseGravity(&opts.Gravity, args[1:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.Gravity.Type == GravitySmart {
|
||||||
|
return fmt.Errorf("%s doesn't support smart gravity", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func applyWidthOption(po *ProcessingOptions, args []string) error {
|
func applyWidthOption(po *ProcessingOptions, args []string) error {
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
return fmt.Errorf("Invalid width arguments: %v", args)
|
return fmt.Errorf("Invalid width arguments: %v", args)
|
||||||
@ -293,23 +315,11 @@ func applyEnlargeOption(po *ProcessingOptions, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func applyExtendOption(po *ProcessingOptions, args []string) error {
|
func applyExtendOption(po *ProcessingOptions, args []string) error {
|
||||||
if len(args) > 4 {
|
return parseExtend(&po.Extend, "extend", args)
|
||||||
return fmt.Errorf("Invalid extend arguments: %v", args)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
po.Extend.Enabled = parseBoolOption(args[0])
|
func applyExtendAspectRatioOption(po *ProcessingOptions, args []string) error {
|
||||||
|
return parseExtend(&po.ExtendAspectRatio, "extend_aspect_ratio", args)
|
||||||
if len(args) > 1 {
|
|
||||||
if err := parseGravity(&po.Extend.Gravity, args[1:]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if po.Extend.Gravity.Type == GravitySmart {
|
|
||||||
return errors.New("extend doesn't support smart gravity")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func applySizeOption(po *ProcessingOptions, args []string) (err error) {
|
func applySizeOption(po *ProcessingOptions, args []string) (err error) {
|
||||||
@ -898,6 +908,8 @@ func applyURLOption(po *ProcessingOptions, name string, args []string) error {
|
|||||||
return applyEnlargeOption(po, args)
|
return applyEnlargeOption(po, args)
|
||||||
case "extend", "ex":
|
case "extend", "ex":
|
||||||
return applyExtendOption(po, args)
|
return applyExtendOption(po, args)
|
||||||
|
case "extend_aspect_ratio", "extend_ar", "exar":
|
||||||
|
return applyExtendAspectRatioOption(po, args)
|
||||||
case "gravity", "g":
|
case "gravity", "g":
|
||||||
return applyGravityOption(po, args)
|
return applyGravityOption(po, args)
|
||||||
case "crop", "c":
|
case "crop", "c":
|
||||||
|
@ -56,13 +56,16 @@ func cropToResult(pctx *pipelineContext, img *vips.Image, po *options.Processing
|
|||||||
resultWidth, resultHeight := resultSize(po)
|
resultWidth, resultHeight := resultSize(po)
|
||||||
|
|
||||||
if po.ResizingType == options.ResizeFillDown {
|
if po.ResizingType == options.ResizeFillDown {
|
||||||
if resultWidth > img.Width() {
|
diffW := float64(resultWidth) / float64(img.Width())
|
||||||
resultHeight = imath.Scale(resultHeight, float64(img.Width())/float64(resultWidth))
|
diffH := float64(resultHeight) / float64(img.Height())
|
||||||
resultWidth = img.Width()
|
|
||||||
}
|
|
||||||
|
|
||||||
if resultHeight > img.Height() {
|
switch {
|
||||||
resultWidth = imath.Scale(resultWidth, float64(img.Height())/float64(resultHeight))
|
case diffW > diffH && diffW > 1.0:
|
||||||
|
resultHeight = imath.Scale(img.Width(), float64(resultHeight)/float64(resultWidth))
|
||||||
|
resultWidth = img.Width()
|
||||||
|
|
||||||
|
case diffH > diffW && diffH > 1.0:
|
||||||
|
resultWidth = imath.Scale(img.Height(), float64(resultWidth)/float64(resultHeight))
|
||||||
resultHeight = img.Height()
|
resultHeight = img.Height()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,17 +2,45 @@ package processing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/imgproxy/imgproxy/v3/imagedata"
|
"github.com/imgproxy/imgproxy/v3/imagedata"
|
||||||
|
"github.com/imgproxy/imgproxy/v3/imath"
|
||||||
"github.com/imgproxy/imgproxy/v3/options"
|
"github.com/imgproxy/imgproxy/v3/options"
|
||||||
"github.com/imgproxy/imgproxy/v3/vips"
|
"github.com/imgproxy/imgproxy/v3/vips"
|
||||||
)
|
)
|
||||||
|
|
||||||
func extend(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
|
func extendImage(img *vips.Image, resultWidth, resultHeight int, opts *options.ExtendOptions, extendAr bool) error {
|
||||||
resultWidth, resultHeight := resultSize(po)
|
if !opts.Enabled || (resultWidth <= img.Width() && resultHeight <= img.Height()) {
|
||||||
|
|
||||||
if !po.Extend.Enabled || (resultWidth <= img.Width() && resultHeight <= img.Height()) {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
offX, offY := calcPosition(resultWidth, resultHeight, img.Width(), img.Height(), &po.Extend.Gravity, false)
|
if extendAr && resultWidth > img.Width() && resultHeight > img.Height() {
|
||||||
|
diffW := float64(resultWidth) / float64(img.Width())
|
||||||
|
diffH := float64(resultHeight) / float64(img.Height())
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case diffH > diffW:
|
||||||
|
resultHeight = imath.Scale(img.Width(), float64(resultHeight)/float64(resultWidth))
|
||||||
|
resultWidth = img.Width()
|
||||||
|
|
||||||
|
case diffW > diffH:
|
||||||
|
resultWidth = imath.Scale(img.Height(), float64(resultWidth)/float64(resultHeight))
|
||||||
|
resultHeight = img.Height()
|
||||||
|
|
||||||
|
default:
|
||||||
|
// The image has the requested arpect ratio
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
offX, offY := calcPosition(resultWidth, resultHeight, img.Width(), img.Height(), &opts.Gravity, false)
|
||||||
return img.Embed(resultWidth, resultHeight, offX, offY)
|
return img.Embed(resultWidth, resultHeight, offX, offY)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extend(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
|
||||||
|
resultWidth, resultHeight := resultSize(po)
|
||||||
|
return extendImage(img, resultWidth, resultHeight, &po.Extend, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func extendAspectRatio(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
|
||||||
|
resultWidth, resultHeight := resultSize(po)
|
||||||
|
return extendImage(img, resultWidth, resultHeight, &po.ExtendAspectRatio, true)
|
||||||
|
}
|
||||||
|
@ -30,6 +30,7 @@ var mainPipeline = pipeline{
|
|||||||
cropToResult,
|
cropToResult,
|
||||||
applyFilters,
|
applyFilters,
|
||||||
extend,
|
extend,
|
||||||
|
extendAspectRatio,
|
||||||
padding,
|
padding,
|
||||||
fixSize,
|
fixSize,
|
||||||
flatten,
|
flatten,
|
||||||
|
Reference in New Issue
Block a user