From e8c69375bf636be0bb4eabd6a45b58eb1fdb877e Mon Sep 17 00:00:00 2001 From: Peter Stace Date: Fri, 16 Sep 2022 13:49:29 +1000 Subject: [PATCH] Check errors from Close method in SavePNG and SaveJPG When closing the file, errors can still occur that might not be surfaced during the writes from `png.Encode` and `jpeg.Encode`. By returning the error from `file.Close()`, these cases can be caught. --- util.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/util.go b/util.go index 4497b7c..051955f 100644 --- a/util.go +++ b/util.go @@ -50,8 +50,10 @@ func SavePNG(path string, im image.Image) error { if err != nil { return err } - defer file.Close() - return png.Encode(file, im) + if err := png.Encode(file, im); err != nil { + return err + } + return file.Close() } func LoadJPG(path string) (image.Image, error) { @@ -68,12 +70,14 @@ func SaveJPG(path string, im image.Image, quality int) error { if err != nil { return err } - defer file.Close() var opt jpeg.Options opt.Quality = quality - return jpeg.Encode(file, im, &opt) + if err := jpeg.Encode(file, im, &opt); err != nil { + return err + } + return file.Close() } func imageToRGBA(src image.Image) *image.RGBA {