MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / determineOutputFormat

Method determineOutputFormat

processing/processing.go:284–328  ·  view source on GitHub ↗

determineOutputFormat determines the output image format based on the processing options and image properties. It modifies the ProcessingOptions in place to set the output format.

(
	img *vips.Image,
	imgdata imagedata.ImageData,
	po ProcessingOptions,
	animated bool,
)

Source from the content-addressed store, hash-verified

282//
283// It modifies the ProcessingOptions in place to set the output format.
284func (p *Processor) determineOutputFormat(
285 img *vips.Image,
286 imgdata imagedata.ImageData,
287 po ProcessingOptions,
288 animated bool,
289) (imagetype.Type, error) {
290 // Check if the image may have transparency
291 expectTransparency := !po.ShouldFlatten() &&
292 (img.HasAlpha() || po.PaddingEnabled() || po.ExtendEnabled())
293
294 format := po.Format()
295
296 switch {
297 case format == imagetype.SVG:
298 // At this point we can't allow requested format to be SVG as we can't save SVGs
299 return imagetype.Unknown, newSaveFormatError(format)
300 case format == imagetype.Unknown:
301 switch {
302 case po.PreferJxl() && !animated:
303 format = imagetype.JXL
304 case po.PreferAvif() && !animated:
305 format = imagetype.AVIF
306 case po.PreferWebP():
307 format = imagetype.WEBP
308 case p.isImageTypePreferred(imgdata.Format()):
309 format = imgdata.Format()
310 default:
311 format = p.findPreferredFormat(animated, expectTransparency)
312 }
313 case po.EnforceJxl() && !animated:
314 format = imagetype.JXL
315 case po.EnforceAvif() && !animated:
316 format = imagetype.AVIF
317 case po.EnforceWebP():
318 format = imagetype.WEBP
319 }
320
321 po.SetFormat(format)
322
323 if !vips.SupportsSave(format) {
324 return format, newSaveFormatError(format)
325 }
326
327 return format, nil
328}
329
330// isImageTypePreferred checks if the given image type is in the list of preferred formats.
331func (p *Processor) isImageTypePreferred(imgtype imagetype.Type) bool {

Callers 1

ProcessImageMethod · 0.95

Calls 15

isImageTypePreferredMethod · 0.95
findPreferredFormatMethod · 0.95
SupportsSaveFunction · 0.92
newSaveFormatErrorFunction · 0.85
ShouldFlattenMethod · 0.80
HasAlphaMethod · 0.80
PaddingEnabledMethod · 0.80
ExtendEnabledMethod · 0.80
PreferJxlMethod · 0.80
PreferAvifMethod · 0.80
PreferWebPMethod · 0.80
EnforceJxlMethod · 0.80

Tested by

no test coverage detected