Last active
August 7, 2019 05:48
-
-
Save maruware/aa82300b65084697b006f50fdc6dae14 to your computer and use it in GitHub Desktop.
[Go] Convert JPEG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ConvertToJpeg(src io.Reader, dst io.Writer, quality int) error { | |
img, _, err := image.Decode(src) | |
if err != nil { | |
return err | |
} | |
opts := &jpeg.Options{Quality: quality} | |
err = jpeg.Encode(dst, img, opts) | |
if err != nil { | |
return err | |
} | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sample(srcImage io.Reader) { | |
pr, pw := io.Pipe() | |
errChan := make(chan error, 1) | |
go func() { | |
defer pw.Close() | |
err := lib.ConvertToJpeg(srcImage, pw, 90) | |
errChan <- err | |
}() | |
err := <- errChan | |
// use pw | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment