How to Convert a GIF to PNG Format in Go
Looking for more transparency and color options for a GIF file? If so, converting it to a PNG may be your best bet. To avoid the extensive and complex process of manually writing the code for the conversion, you can use the following API in Go to automatically convert the GIF to PNG with no extra effort required; the transparency of your original file is preserved when present but can be altered once in the new format.
Now we can initiate the image conversion by simply inputting the target file and API key into the following code:
package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/image/convert/to/png"
method := "POST"payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("imageFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
Process complete! Need an API key? Head over to the Cloudmersive website to register for a free account; this will give you access to 800 monthly calls across our entire library of APIs.