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

Method saveImage

processing/processing.go:498–532  ·  view source on GitHub ↗
(
	ctx context.Context,
	img *vips.Image,
	po ProcessingOptions,
)

Source from the content-addressed store, hash-verified

496}
497
498func (p *Processor) saveImage(
499 ctx context.Context,
500 img *vips.Image,
501 po ProcessingOptions,
502) (imagedata.ImageData, error) {
503 outFormat := po.Format()
504
505 // AVIF has a minimal dimension of 16 pixels.
506 // If one of the dimensions is less, we need to switch to another format.
507 if outFormat == imagetype.AVIF && (img.Width() < 16 || img.Height() < 16) {
508 if img.HasAlpha() {
509 outFormat = imagetype.PNG
510 } else {
511 outFormat = imagetype.JPEG
512 }
513
514 po.SetFormat(outFormat)
515
516 slog.Warn(fmt.Sprintf(
517 "Minimal dimension of AVIF is 16, current image size is %dx%d. Image will be saved as %s",
518 img.Width(), img.Height(), outFormat,
519 ))
520 }
521
522 quality := po.Quality(outFormat)
523
524 // If we want and can fit the image into the specified number of bytes,
525 // let's do it.
526 if maxBytes := po.MaxBytes(); maxBytes > 0 && outFormat.SupportsQuality() {
527 return saveImageToFitBytes(ctx, img, outFormat, quality, maxBytes, po.Options)
528 }
529
530 // Otherwise, just save the image with the specified quality.
531 return img.Save(outFormat, quality, po.Options)
532}

Callers 1

ProcessImageMethod · 0.95

Calls 11

saveImageToFitBytesFunction · 0.85
HasAlphaMethod · 0.80
SetFormatMethod · 0.80
WarnMethod · 0.80
QualityMethod · 0.80
MaxBytesMethod · 0.80
SupportsQualityMethod · 0.80
SaveMethod · 0.80
FormatMethod · 0.65
WidthMethod · 0.45
HeightMethod · 0.45

Tested by

no test coverage detected