( ctx context.Context, img *vips.Image, po ProcessingOptions, )
| 496 | } |
| 497 | |
| 498 | func (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 | } |
no test coverage detected