mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-04-12 06:58:15 +02:00
Quality processing option
This commit is contained in:
parent
61ae01b0a4
commit
c043716ac1
@ -53,7 +53,7 @@ When you use imgproxy in a development environment, it can be useful to ignore S
|
|||||||
|
|
||||||
### Compression
|
### Compression
|
||||||
|
|
||||||
* `IMGPROXY_QUALITY`: quality of the resulting image, percentage. Default: `80`;
|
* `IMGPROXY_QUALITY`: default quality of the resulting image, percentage. Default: `80`;
|
||||||
* `IMGPROXY_GZIP_COMPRESSION`: GZip compression level. Default: `5`;
|
* `IMGPROXY_GZIP_COMPRESSION`: GZip compression level. Default: `5`;
|
||||||
* `IMGPROXY_JPEG_PROGRESSIVE` : when true, enables progressive JPEG compression. Default: false;
|
* `IMGPROXY_JPEG_PROGRESSIVE` : when true, enables progressive JPEG compression. Default: false;
|
||||||
* `IMGPROXY_PNG_INTERLACED`: when true, enables interlaced PNG compression. Default: false;
|
* `IMGPROXY_PNG_INTERLACED`: when true, enables interlaced PNG compression. Default: false;
|
||||||
|
@ -119,6 +119,17 @@ When imgproxy needs to cut some parts of the image, it is guided by the gravity.
|
|||||||
|
|
||||||
Default: `ce`
|
Default: `ce`
|
||||||
|
|
||||||
|
##### Quality
|
||||||
|
|
||||||
|
```
|
||||||
|
quality:%quality
|
||||||
|
q:%quality
|
||||||
|
```
|
||||||
|
|
||||||
|
Redefines quality of the resulting image, percentage.
|
||||||
|
|
||||||
|
Default: value from the environment variable.
|
||||||
|
|
||||||
##### Background
|
##### Background
|
||||||
|
|
||||||
```
|
```
|
||||||
|
11
process.go
11
process.go
@ -28,7 +28,6 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type cConfig struct {
|
type cConfig struct {
|
||||||
Quality C.int
|
|
||||||
JpegProgressive C.int
|
JpegProgressive C.int
|
||||||
PngInterlaced C.int
|
PngInterlaced C.int
|
||||||
WatermarkOpacity C.double
|
WatermarkOpacity C.double
|
||||||
@ -83,8 +82,6 @@ func initVips() {
|
|||||||
vipsTypeSupportSave[imageTypeWEBP] = true
|
vipsTypeSupportSave[imageTypeWEBP] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
cConf.Quality = C.int(conf.Quality)
|
|
||||||
|
|
||||||
if conf.JpegProgressive {
|
if conf.JpegProgressive {
|
||||||
cConf.JpegProgressive = C.int(1)
|
cConf.JpegProgressive = C.int(1)
|
||||||
}
|
}
|
||||||
@ -389,7 +386,7 @@ func processImage(ctx context.Context) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return vipsSaveImage(img, po.Format)
|
return vipsSaveImage(img, po.Format, po.Quality)
|
||||||
}
|
}
|
||||||
|
|
||||||
func vipsPrepareWatermark() error {
|
func vipsPrepareWatermark() error {
|
||||||
@ -458,7 +455,7 @@ func vipsLoadImage(data []byte, imgtype imageType, shrink int) (*C.struct__VipsI
|
|||||||
return img, nil
|
return img, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func vipsSaveImage(img *C.struct__VipsImage, imgtype imageType) ([]byte, error) {
|
func vipsSaveImage(img *C.struct__VipsImage, imgtype imageType, quality int) ([]byte, error) {
|
||||||
var ptr unsafe.Pointer
|
var ptr unsafe.Pointer
|
||||||
defer C.g_free_go(&ptr)
|
defer C.g_free_go(&ptr)
|
||||||
|
|
||||||
@ -468,11 +465,11 @@ func vipsSaveImage(img *C.struct__VipsImage, imgtype imageType) ([]byte, error)
|
|||||||
|
|
||||||
switch imgtype {
|
switch imgtype {
|
||||||
case imageTypeJPEG:
|
case imageTypeJPEG:
|
||||||
err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, cConf.Quality, cConf.JpegProgressive)
|
err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(quality), cConf.JpegProgressive)
|
||||||
case imageTypePNG:
|
case imageTypePNG:
|
||||||
err = C.vips_pngsave_go(img, &ptr, &imgsize, cConf.PngInterlaced)
|
err = C.vips_pngsave_go(img, &ptr, &imgsize, cConf.PngInterlaced)
|
||||||
case imageTypeWEBP:
|
case imageTypeWEBP:
|
||||||
err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, cConf.Quality)
|
err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, C.int(quality))
|
||||||
}
|
}
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return nil, vipsError()
|
return nil, vipsError()
|
||||||
|
@ -113,6 +113,7 @@ type processingOptions struct {
|
|||||||
Gravity gravityOptions
|
Gravity gravityOptions
|
||||||
Enlarge bool
|
Enlarge bool
|
||||||
Format imageType
|
Format imageType
|
||||||
|
Quality int
|
||||||
Flatten bool
|
Flatten bool
|
||||||
Background color
|
Background color
|
||||||
Blur float32
|
Blur float32
|
||||||
@ -345,6 +346,20 @@ func applyGravityOption(po *processingOptions, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func applyQualityOption(po *processingOptions, args []string) error {
|
||||||
|
if len(args) > 1 {
|
||||||
|
return fmt.Errorf("Invalid quality arguments: %v", args)
|
||||||
|
}
|
||||||
|
|
||||||
|
if q, err := strconv.Atoi(args[0]); err == nil && q > 0 && q <= 100 {
|
||||||
|
po.Quality = q
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("Invalid quality: %s", args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func applyBackgroundOption(po *processingOptions, args []string) error {
|
func applyBackgroundOption(po *processingOptions, args []string) error {
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 1:
|
case 1:
|
||||||
@ -549,6 +564,10 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
|
|||||||
if err := applyGravityOption(po, args); err != nil {
|
if err := applyGravityOption(po, args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
case "quality", "q":
|
||||||
|
if err := applyQualityOption(po, args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
case "background", "bg":
|
case "background", "bg":
|
||||||
if err := applyBackgroundOption(po, args); err != nil {
|
if err := applyBackgroundOption(po, args); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -625,6 +644,7 @@ func defaultProcessingOptions(acceptHeader string) (*processingOptions, error) {
|
|||||||
Height: 0,
|
Height: 0,
|
||||||
Gravity: gravityOptions{Type: gravityCenter},
|
Gravity: gravityOptions{Type: gravityCenter},
|
||||||
Enlarge: false,
|
Enlarge: false,
|
||||||
|
Quality: conf.Quality,
|
||||||
Format: imageTypeJPEG,
|
Format: imageTypeJPEG,
|
||||||
Blur: 0,
|
Blur: 0,
|
||||||
Sharpen: 0,
|
Sharpen: 0,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user