func (handler *ImageUtilsHandler) Save(img image.Image, format string) ([]byte, error) { // Based on github.com/disintegration/imaging/helpers.go Save() var err error writer := new(bytes.Buffer) switch format { case ".jpg", ".jpeg": var rgba *image.RGBA if nrgba, ok := img.(*image.NRGBA); ok { if nrgba.Opaque() { rgba = &image.RGBA{ Pix: nrgba.Pix, Stride: nrgba.Stride, Rect: nrgba.Rect, } } } if rgba != nil { err = jpeg.Encode(writer, rgba, &jpeg.Options{Quality: 95}) } else { err = jpeg.Encode(writer, img, &jpeg.Options{Quality: 95}) } case ".png": err = png.Encode(writer, img) case ".tif", ".tiff": err = tiff.Encode(writer, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true}) case ".bmp": err = bmp.Encode(writer, img) default: return nil, errors.New("Invalid image format") } return writer.Bytes(), err }
// Save saves the image to file with the specified filename. // The format is determined from the filename extension: "jpg" (or "jpeg"), "png", "tif" (or "tiff") and "bmp" are supported. func Save(img image.Image, filename string) (err error) { format := strings.ToLower(filepath.Ext(filename)) okay := false for _, ext := range []string{".jpg", ".jpeg", ".png", ".tif", ".tiff", ".bmp"} { if format == ext { okay = true break } } if !okay { return fmt.Errorf(`imaging: unsupported image format: "%s"`, format) } file, err := os.Create(filename) if err != nil { return } defer file.Close() switch format { case ".jpg", ".jpeg": var rgba *image.RGBA if nrgba, ok := img.(*image.NRGBA); ok { if nrgba.Opaque() { rgba = &image.RGBA{ Pix: nrgba.Pix, Stride: nrgba.Stride, Rect: nrgba.Rect, } } } if rgba != nil { err = jpeg.Encode(file, rgba, &jpeg.Options{Quality: 95}) } else { err = jpeg.Encode(file, img, &jpeg.Options{Quality: 95}) } case ".png": err = png.Encode(file, img) case ".tif", ".tiff": err = tiff.Encode(file, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true}) case ".bmp": err = bmp.Encode(file, img) } return }
// Save saves the image to file with the specified filename. // The format is determined from the filename extension: "jpg" (or "jpeg"), "png", "tif" (or "tiff"), "bmp", and "gif" are supported. func Save(img image.Image, filename string) (err error) { format := strings.ToLower(filepath.Ext(filename)) m, err := regexp.MatchString(`^\.(jpg|jpeg|png|tif|tiff|bmp|gif)$`, format) if err != nil || !m { err = fmt.Errorf(`imaging: unsupported image format: "%s"`, format) return } file, err := os.Create(filename) if err != nil { return } defer file.Close() switch format { case ".jpg", ".jpeg": var rgba *image.RGBA if nrgba, ok := img.(*image.NRGBA); ok { if nrgba.Opaque() { rgba = &image.RGBA{ Pix: nrgba.Pix, Stride: nrgba.Stride, Rect: nrgba.Rect, } } } if rgba != nil { err = jpeg.Encode(file, rgba, &jpeg.Options{Quality: 95}) } else { err = jpeg.Encode(file, img, &jpeg.Options{Quality: 95}) } case ".png": err = png.Encode(file, img) case ".tif", ".tiff": err = tiff.Encode(file, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true}) case ".bmp": err = bmp.Encode(file, img) case ".gif": err = gif.Encode(file, img, &gif.Options{NumColors: 256})) default: err = fmt.Errorf(`imaging: unsupported image format: "%s"`, format) } return }