diff --git a/docs/generating_the_url_advanced.md b/docs/generating_the_url_advanced.md index d1335b6c..098d3142 100644 --- a/docs/generating_the_url_advanced.md +++ b/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 diff --git a/docs/generating_the_url_basic.md b/docs/generating_the_url_basic.md index e8b196d8..97012d8f 100644 --- a/docs/generating_the_url_basic.md +++ b/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 diff --git a/process.go b/process.go index 387b356d..46e6c070 100644 --- a/process.go +++ b/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 diff --git a/processing_options.go b/processing_options.go index c7e8bbc6..cffce80c 100644 --- a/processing_options.go +++ b/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}, diff --git a/processing_options_test.go b/processing_options_test.go index 6b196560..649771d9 100644 --- a/processing_options_test.go +++ b/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"