extend option

This commit is contained in:
DarthSim
2019-02-21 21:55:20 +06:00
parent f43fd6eec0
commit 5040ba7d61
5 changed files with 76 additions and 12 deletions

View File

@@ -34,20 +34,20 @@ imgproxy supports the following processing options:
##### Resize ##### Resize
``` ```
resize:%resizing_type:%width:%height:%enlarge resize:%resizing_type:%width:%height:%enlarge:%extend
rs:%resizing_type:%width:%height:%enlarge rs:%resizing_type:%width:%height:%enlarge:%extend
``` ```
Meta-option that defines the [resizing type](#resizing-type), [width](#width), [height](#height), and [enlarge](#enlarge). All arguments are optional and can be omited to use their default values. Meta-option that defines the [resizing type](#resizing-type), [width](#width), [height](#height), [enlarge](#enlarge), and [extend](#extend). All arguments are optional and can be omited to use their default values.
##### Size ##### Size
``` ```
size:%width:%height:%enlarge size:%width:%height:%enlarge:%extend
s:%width:%height:%enlarge s:%width:%height:%enlarge:%extend
``` ```
Meta-option that defines the [width](#width), [height](#height), and [enlarge](#enlarge). All arguments are optional and can be omited to use their default values. Meta-option that defines the [width](#width), [height](#height), [enlarge](#enlarge), and [extend](#extend). All arguments are optional and can be omited to use their default values.
##### Resizing type ##### Resizing type
@@ -107,6 +107,17 @@ If set to `0`, imgproxy will not enlarge the image if it is smaller than the giv
Default: `0` Default: `0`
##### Extend
```
extend:%extend
ex:%extend
```
If set to `0`, imgproxy will not extend the image if the resizing result is smaller than the given size. With any other value, imgproxy will extend the image to the given size.
Default: `0`
##### Gravity ##### Gravity
``` ```

View File

@@ -389,6 +389,18 @@ func transformImage(ctx context.Context, img **C.VipsImage, data []byte, po *pro
} }
} }
if po.Expand && (po.Width > int((*img).Xsize) || po.Height > int((*img).Ysize)) {
if err = vipsEnsureAlpha(img); err != nil {
return err
}
hasAlpha = true
if err = vipsEmbed(img, gravityCenter, C.int(po.Width), C.int(po.Height), 0, 0); err != nil {
return err
}
}
if hasAlpha && (po.Flatten || po.Format == imageTypeJPEG) { if hasAlpha && (po.Flatten || po.Format == imageTypeJPEG) {
if err = vipsFlatten(img, po.Background); err != nil { if err = vipsFlatten(img, po.Background); err != nil {
return err return err
@@ -769,6 +781,17 @@ func vipsSmartCrop(img **C.VipsImage, width, height int) error {
return nil return nil
} }
func vipsEnsureAlpha(img **C.VipsImage) error {
var tmp *C.VipsImage
if C.vips_ensure_alpha(*img, &tmp) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsFlatten(img **C.VipsImage, bg rgbColor) error { func vipsFlatten(img **C.VipsImage, bg rgbColor) error {
var tmp *C.VipsImage var tmp *C.VipsImage

View File

@@ -124,6 +124,7 @@ type processingOptions struct {
Dpr float64 Dpr float64
Gravity gravityOptions Gravity gravityOptions
Enlarge bool Enlarge bool
Expand bool
Format imageType Format imageType
Quality int Quality int
Flatten bool Flatten bool
@@ -322,8 +323,18 @@ func applyEnlargeOption(po *processingOptions, args []string) error {
return nil return nil
} }
func applyExtendOption(po *processingOptions, args []string) error {
if len(args) > 1 {
return fmt.Errorf("Invalid expand arguments: %v", args)
}
po.Expand = args[0] != "0"
return nil
}
func applySizeOption(po *processingOptions, args []string) (err error) { func applySizeOption(po *processingOptions, args []string) (err error) {
if len(args) > 3 { if len(args) > 4 {
return fmt.Errorf("Invalid size arguments: %v", args) return fmt.Errorf("Invalid size arguments: %v", args)
} }
@@ -339,12 +350,18 @@ func applySizeOption(po *processingOptions, args []string) (err error) {
} }
} }
if len(args) == 3 && len(args[2]) > 0 { if len(args) >= 3 && len(args[2]) > 0 {
if err = applyEnlargeOption(po, args[2:3]); err != nil { if err = applyEnlargeOption(po, args[2:3]); err != nil {
return return
} }
} }
if len(args) == 4 && len(args[3]) > 0 {
if err = applyExtendOption(po, args[3:4]); err != nil {
return
}
}
return nil return nil
} }
@@ -363,7 +380,7 @@ func applyResizingTypeOption(po *processingOptions, args []string) error {
} }
func applyResizeOption(po *processingOptions, args []string) error { func applyResizeOption(po *processingOptions, args []string) error {
if len(args) > 4 { if len(args) > 5 {
return fmt.Errorf("Invalid resize arguments: %v", args) return fmt.Errorf("Invalid resize arguments: %v", args)
} }
@@ -640,6 +657,10 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
if err := applyEnlargeOption(po, args); err != nil { if err := applyEnlargeOption(po, args); err != nil {
return err return err
} }
case "extend", "ex":
if err := applyExtendOption(po, args); err != nil {
return err
}
case "dpr": case "dpr":
if err := applyDprOption(po, args); err != nil { if err := applyDprOption(po, args); err != nil {
return err return err

13
vips.c
View File

@@ -288,10 +288,17 @@ vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int heigh
} }
int int
vips_apply_opacity(VipsImage *in, VipsImage **out, double opacity){ vips_ensure_alpha(VipsImage *in, VipsImage **out) {
gboolean has_alpha = vips_image_hasalpha_go(in); if (vips_image_hasalpha_go(in)) {
return vips_copy(in, out, NULL);
}
if (has_alpha) { return vips_bandjoin_const1(in, out, 255, NULL);
}
int
vips_apply_opacity(VipsImage *in, VipsImage **out, double opacity){
if (vips_image_hasalpha_go(in)) {
if (opacity < 1) { if (opacity < 1) {
VipsImage *img, *img_alpha, *tmp; VipsImage *img, *img_alpha, *tmp;

2
vips.h
View File

@@ -63,7 +63,9 @@ int vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b
int vips_replicate_go(VipsImage *in, VipsImage **out, int across, int down); int vips_replicate_go(VipsImage *in, VipsImage **out, int across, int down);
int vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height); int vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height);
int vips_ensure_alpha(VipsImage *in, VipsImage **out);
int vips_apply_opacity(VipsImage *in, VipsImage **out, double opacity); int vips_apply_opacity(VipsImage *in, VipsImage **out, double opacity);
int vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity); int vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity);
int vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n); int vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n);