Parcourir la source

Progressive JPEG & interlaced PNG support

DarthSim il y a 6 ans
Parent
commit
c3d39b2b42
4 fichiers modifiés avec 29 ajouts et 5 suppressions
  1. 2 0
      README.md
  2. 4 0
      config.go
  3. 21 3
      process.go
  4. 2 2
      vips.h

+ 2 - 0
README.md

@@ -163,6 +163,8 @@ You can also specify a secret to enable authorization with the HTTP `Authorizati
 
 
 * `IMGPROXY_QUALITY` — quality of the resulting image, percentage. Default: `80`;
 * `IMGPROXY_QUALITY` — 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 compression of JPEG. Default: false;
+* `IMGPROXY_PNG_INTERLACED` — when true, enables interlaced compression of PNG. Default: false;
 
 
 #### Miscellaneous
 #### Miscellaneous
 
 

+ 4 - 0
config.go

@@ -87,6 +87,8 @@ type config struct {
 	MaxSrcDimension  int
 	MaxSrcDimension  int
 	MaxSrcResolution int
 	MaxSrcResolution int
 
 
+	JpegProgressive bool
+	PngInterlaced   bool
 	Quality         int
 	Quality         int
 	GZipCompression int
 	GZipCompression int
 
 
@@ -140,6 +142,8 @@ func init() {
 	intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
 	intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
 	megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
 	megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
 
 
+	boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
+	boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED")
 	intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
 	intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
 	intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
 	intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
 
 

+ 21 - 3
process.go

@@ -81,6 +81,14 @@ var vipsSupportSmartcrop bool
 var vipsTypeSupportLoad = make(map[imageType]bool)
 var vipsTypeSupportLoad = make(map[imageType]bool)
 var vipsTypeSupportSave = make(map[imageType]bool)
 var vipsTypeSupportSave = make(map[imageType]bool)
 
 
+type cConfig struct {
+	Quality         C.int
+	JpegProgressive C.int
+	PngInterlaced   C.int
+}
+
+var cConf cConfig
+
 func initVips() {
 func initVips() {
 	runtime.LockOSThread()
 	runtime.LockOSThread()
 	defer runtime.UnlockOSThread()
 	defer runtime.UnlockOSThread()
@@ -125,6 +133,16 @@ func initVips() {
 	if int(C.vips_type_find_save_go(C.WEBP)) != 0 {
 	if int(C.vips_type_find_save_go(C.WEBP)) != 0 {
 		vipsTypeSupportSave[WEBP] = true
 		vipsTypeSupportSave[WEBP] = true
 	}
 	}
+
+	cConf.Quality = C.int(conf.Quality)
+
+	if conf.JpegProgressive {
+		cConf.JpegProgressive = C.int(1)
+	}
+
+	if conf.PngInterlaced {
+		cConf.PngInterlaced = C.int(1)
+	}
 }
 }
 
 
 func shutdownVips() {
 func shutdownVips() {
@@ -366,11 +384,11 @@ func vipsSaveImage(img *C.struct__VipsImage, imgtype imageType) ([]byte, error)
 
 
 	switch imgtype {
 	switch imgtype {
 	case JPEG:
 	case JPEG:
-		err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0)
+		err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, cConf.Quality, cConf.JpegProgressive)
 	case PNG:
 	case PNG:
-		err = C.vips_pngsave_go(img, &ptr, &imgsize)
+		err = C.vips_pngsave_go(img, &ptr, &imgsize, cConf.PngInterlaced)
 	case WEBP:
 	case WEBP:
-		err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality))
+		err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, cConf.Quality)
 	}
 	}
 	if err != 0 {
 	if err != 0 {
 		return nil, vipsError()
 		return nil, vipsError()

+ 2 - 2
vips.h

@@ -198,8 +198,8 @@ vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality,
 }
 }
 
 
 int
 int
-vips_pngsave_go(VipsImage *in, void **buf, size_t *len) {
-  return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
+vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace) {
+  return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
 }
 }
 
 
 int
 int