1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-03-17 20:17:48 +02:00

Don't switch to autodetected format if source one supports animation but autodetected one doesn't

This commit is contained in:
DarthSim 2021-01-18 22:53:49 +06:00
parent bac349558c
commit be26e10ce5
2 changed files with 13 additions and 4 deletions

View File

@ -132,6 +132,8 @@ imgproxy can use the `Accept` HTTP header to detect if the browser supports AVIF
**📝Note:** imgproxy prefers AVIF over WebP. This means that if both AVIF and WebP detection/enforcement are enabled and the browser supports both of them, AVIF will be used.
**📝Note:** If both the source and the requested image formats support animation and AVIF detection/enforcement is enabled, AVIF won't be used as AVIF sequence is not supported yet.
**📝Note:** When AVIF/WebP support detection is enabled, please take care to configure your CDN or caching proxy to take the `Accept` HTTP header into account while caching.
**⚠️Warning:** Headers cannot be signed. This means that an attacker can bypass your CDN cache by changing the `Accept` HTTP headers. Have this in mind when configuring your production caching setup.

View File

@ -34,6 +34,13 @@ func imageTypeGoodForWeb(imgtype imageType) bool {
imgtype != imageTypeBMP
}
func canSwitchFormat(src, dst, want imageType) bool {
return imageTypeSaveSupport(want) &&
(!vipsSupportAnimation(src) ||
(dst != imageTypeUnknown && !vipsSupportAnimation(dst)) ||
vipsSupportAnimation(want))
}
func extractMeta(img *vipsImage, baseAngle int, useOrientation bool) (int, int, int, bool) {
width := img.Width()
height := img.Height()
@ -745,18 +752,18 @@ func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
switch {
case po.Format == imageTypeUnknown:
switch {
case po.PreferAvif && imageTypeSaveSupport(imageTypeAVIF):
case po.PreferAvif && canSwitchFormat(imgdata.Type, imageTypeUnknown, imageTypeAVIF):
po.Format = imageTypeAVIF
case po.PreferWebP && imageTypeSaveSupport(imageTypeWEBP):
case po.PreferWebP && canSwitchFormat(imgdata.Type, imageTypeUnknown, imageTypeWEBP):
po.Format = imageTypeWEBP
case imageTypeSaveSupport(imgdata.Type) && imageTypeGoodForWeb(imgdata.Type):
po.Format = imgdata.Type
default:
po.Format = imageTypeJPEG
}
case po.EnforceAvif && imageTypeSaveSupport(imageTypeAVIF):
case po.EnforceAvif && canSwitchFormat(imgdata.Type, po.Format, imageTypeAVIF):
po.Format = imageTypeAVIF
case po.EnforceWebP && imageTypeSaveSupport(imageTypeWEBP):
case po.EnforceWebP && canSwitchFormat(imgdata.Type, po.Format, imageTypeWEBP):
po.Format = imageTypeWEBP
}