Browse Source

Use source image format as resulting one dy default

DarthSim 6 years ago
parent
commit
da1a802632

+ 1 - 1
docs/generating_the_url_advanced.md

@@ -275,7 +275,7 @@ Extension specifies the format of the resulting image. At the moment, imgproxy s
 
 **Note:** Read about GIF support [here](./image_formats_support.md#gif-support).
 
-The extension part can be omitted. In this case, if the format is not defined by processing options, imgproxy will use `jpg` by default. You can also [enable WebP support detection](./configuration.md#webp-support-detection) to use it as default resulting format when possible.
+The extension part can be omitted. In this case, imgproxy will use source image format as resulting one. If source image format is not supported as resulting, imgproxy will use `jpg`. You also can [enable WebP support detection](./configuration.md#webp-support-detection) to use it as default resulting format when possible.
 
 ### Example
 

+ 1 - 1
docs/generating_the_url_basic.md

@@ -91,7 +91,7 @@ Extension specifies the format of the resulting image. At the moment, imgproxy s
 
 **Note:** Read about GIF support [here](./image_formats_support.md#gif-support).
 
-The extension part can be omitted. In this case, imgproxy will use `jpg` by default. You also can [enable WebP support detection](./configuration.md#webp-support-detection) to use it as default resulting format when possible.
+The extension part can be omitted. In this case, imgproxy will use source image format as resulting one. If source image format is not supported as resulting, imgproxy will use `jpg`. You also can [enable WebP support detection](./configuration.md#webp-support-detection) to use it as default resulting format when possible.
 
 ### Example
 

+ 9 - 2
process.go

@@ -450,10 +450,9 @@ func processImage(ctx context.Context) ([]byte, error) {
 		defer startPrometheusDuration(prometheusProcessingDuration)()
 	}
 
-	po := getProcessingOptions(ctx)
-
 	defer C.vips_cleanup()
 
+	po := getProcessingOptions(ctx)
 	data := getImageData(ctx).Bytes()
 	imgtype := getImageType(ctx)
 
@@ -461,6 +460,14 @@ func processImage(ctx context.Context) ([]byte, error) {
 		return nil, errSmartCropNotSupported
 	}
 
+	if po.Format == imageTypeUnknown {
+		if vipsTypeSupportSave[imgtype] {
+			po.Format = imgtype
+		} else {
+			po.Format = imageTypeJPEG
+		}
+	}
+
 	img, err := vipsLoadImage(data, imgtype, 1, po.Format == imageTypeGIF)
 	if err != nil {
 		return nil, err

+ 1 - 1
processing_options.go

@@ -701,7 +701,7 @@ func defaultProcessingOptions(headers *processingHeaders) (*processingOptions, e
 		Gravity:     gravityOptions{Type: gravityCenter},
 		Enlarge:     false,
 		Quality:     conf.Quality,
-		Format:      imageTypeJPEG,
+		Format:      imageTypeUnknown,
 		Blur:        0,
 		Sharpen:     0,
 		Watermark:   watermarkOptions{Opacity: 1, Replicate: false, Gravity: gravityCenter},

+ 2 - 2
processing_options_test.go

@@ -37,7 +37,7 @@ func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithoutExtension() {
 
 	require.Nil(s.T(), err)
 	assert.Equal(s.T(), imageURL, getImageURL(ctx))
-	assert.Equal(s.T(), imageTypeJPEG, getProcessingOptions(ctx).Format)
+	assert.Equal(s.T(), imageTypeUnknown, getProcessingOptions(ctx).Format)
 }
 
 func (s *ProcessingOptionsTestSuite) TestParseBase64URLWithBase() {
@@ -78,7 +78,7 @@ func (s *ProcessingOptionsTestSuite) TestParsePlainURLWithoutExtension() {
 
 	require.Nil(s.T(), err)
 	assert.Equal(s.T(), imageURL, getImageURL(ctx))
-	assert.Equal(s.T(), imageTypeJPEG, getProcessingOptions(ctx).Format)
+	assert.Equal(s.T(), imageTypeUnknown, getProcessingOptions(ctx).Format)
 }
 func (s *ProcessingOptionsTestSuite) TestParsePlainURLEscaped() {
 	imageURL := "http://images.dev/lorem/ipsum.jpg?param=value"