pickSmallestEncoding encodes the image as both PNG and JPEG and returns whichever is smaller.
(img image.Image)
| 257 | |
| 258 | // pickSmallestEncoding encodes the image as both PNG and JPEG and returns whichever is smaller. |
| 259 | func pickSmallestEncoding(img image.Image) ([]byte, string, error) { |
| 260 | pngData, errPNG := encodePNG(img) |
| 261 | jpegData, errJPEG := encodeJPEG(img, jpegQuality) |
| 262 | if errPNG != nil && errJPEG != nil { |
| 263 | return nil, "", errors.Join(errPNG, errJPEG) |
| 264 | } |
| 265 | if errPNG != nil { |
| 266 | return jpegData, "image/jpeg", nil |
| 267 | } |
| 268 | if errJPEG != nil { |
| 269 | return pngData, "image/png", nil |
| 270 | } |
| 271 | |
| 272 | if len(pngData) <= len(jpegData) { |
| 273 | return pngData, "image/png", nil |
| 274 | } |
| 275 | return jpegData, "image/jpeg", nil |
| 276 | } |
| 277 | |
| 278 | func encodePNG(img image.Image) ([]byte, error) { |
| 279 | var buf bytes.Buffer |
no test coverage detected