Browse Source

Quality processing option

DarthSim 6 năm trước cách đây
mục cha
commit
c043716ac1
4 tập tin đã thay đổi với 36 bổ sung8 xóa
  1. 1 1
      docs/configuration.md
  2. 11 0
      docs/generating_the_url_advanced.md
  3. 4 7
      process.go
  4. 20 0
      processing_options.go

+ 1 - 1
docs/configuration.md

@@ -53,7 +53,7 @@ When you use imgproxy in a development environment, it can be useful to ignore S
 
 ### 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_JPEG_PROGRESSIVE` : when true, enables progressive JPEG compression. Default: false;
 * `IMGPROXY_PNG_INTERLACED`: when true, enables interlaced PNG compression. Default: false;

+ 11 - 0
docs/generating_the_url_advanced.md

@@ -119,6 +119,17 @@ When imgproxy needs to cut some parts of the image, it is guided by the gravity.
 
 Default: `ce`
 
+##### Quality
+
+```
+quality:%quality
+q:%quality
+```
+
+Redefines quality of the resulting image, percentage.
+
+Default: value from the environment variable.
+
 ##### Background
 
 ```

+ 4 - 7
process.go

@@ -28,7 +28,6 @@ var (
 )
 
 type cConfig struct {
-	Quality          C.int
 	JpegProgressive  C.int
 	PngInterlaced    C.int
 	WatermarkOpacity C.double
@@ -83,8 +82,6 @@ func initVips() {
 		vipsTypeSupportSave[imageTypeWEBP] = true
 	}
 
-	cConf.Quality = C.int(conf.Quality)
-
 	if conf.JpegProgressive {
 		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 {
@@ -458,7 +455,7 @@ func vipsLoadImage(data []byte, imgtype imageType, shrink int) (*C.struct__VipsI
 	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
 	defer C.g_free_go(&ptr)
 
@@ -468,11 +465,11 @@ func vipsSaveImage(img *C.struct__VipsImage, imgtype imageType) ([]byte, error)
 
 	switch imgtype {
 	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:
 		err = C.vips_pngsave_go(img, &ptr, &imgsize, cConf.PngInterlaced)
 	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 {
 		return nil, vipsError()

+ 20 - 0
processing_options.go

@@ -113,6 +113,7 @@ type processingOptions struct {
 	Gravity    gravityOptions
 	Enlarge    bool
 	Format     imageType
+	Quality    int
 	Flatten    bool
 	Background color
 	Blur       float32
@@ -345,6 +346,20 @@ func applyGravityOption(po *processingOptions, args []string) error {
 	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 {
 	switch len(args) {
 	case 1:
@@ -549,6 +564,10 @@ func applyProcessingOption(po *processingOptions, name string, args []string) er
 		if err := applyGravityOption(po, args); err != nil {
 			return err
 		}
+	case "quality", "q":
+		if err := applyQualityOption(po, args); err != nil {
+			return err
+		}
 	case "background", "bg":
 		if err := applyBackgroundOption(po, args); err != nil {
 			return err
@@ -625,6 +644,7 @@ func defaultProcessingOptions(acceptHeader string) (*processingOptions, error) {
 		Height:      0,
 		Gravity:     gravityOptions{Type: gravityCenter},
 		Enlarge:     false,
+		Quality:     conf.Quality,
 		Format:      imageTypeJPEG,
 		Blur:        0,
 		Sharpen:     0,