Created
May 11, 2019 03:08
-
-
Save jonwinton/13d9172310b2952859bf061f21212c48 to your computer and use it in GitHub Desktop.
Adventures In Golang: Animated Gif Functional
This file contains hidden or 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 sendGif(c echo.Context) error { | |
| // Initialize a Buffer (https://golang.org/pkg/bytes/#Buffer) | |
| // This is where we'll write gif data too so that it can be | |
| // read from via the http server | |
| buf := new(bytes.Buffer) | |
| // Grab the file from the filesystem | |
| imgFile, _ := os.Open("cool.gif") | |
| // We want to break apart all the frame data to make sure we | |
| // get all the animation in (https://golang.org/pkg/image/gif/#DecodeAll) | |
| // This will return the GIF type which could be modified to upate | |
| // how the gif behaves (https://golang.org/pkg/image/gif/#GIF) | |
| g, _ := gif.DecodeAll(imgFile) | |
| // Now that we have all the frame data we want to write all of the data | |
| // to our Buffer which the server can use to send back to the client | |
| // requesting the image | |
| err := gif.EncodeAll(buf, g) | |
| // Print an error | |
| if err != nil { | |
| fmt.Println(err) | |
| return echo.NewHTTPError(http.StatusInternalServerError, "An issue occured, check the server logs") | |
| } | |
| // This is specific to the Echo framework, it just sends the response | |
| // back to the client, but this Buffer will have all the frames | |
| return c.Stream(http.StatusOK, "image/gif", buf) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment