How to Autodetect & Convert a Document to a Thumbnail Image Object using Go
Having a standardized, programmatic way of generating thumbnails is a huge help for content creation/management professionals. Our Document to Thumbnail API allows you to convert dozens of common Office & Image document formats into a PNG image with customizable dimensions, making it easy to create your own suite of thumbnails at scale. This API enables you to specify the maximum height and width of your output file (default is 128x128 pixels; maximum is 2048x2048 pixels), and it also allows you to specify your file extension ahead of the operation (this may help improve API response time in some cases). Using this API is simple & free: just follow instructions below to structure your API call using Go code examples from our API console and register a free account on our website to get your Cloudmersive API key to authenticate the service.
To call this API, let’s simply copy the code below into our file, configure our file & API key headers, and set our height/width by entering valid integers into the width & height fields:
package main
import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.cloudmersive.com/convert/autodetect/to/thumbnail/advanced"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",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("pages", "<integer>")
req.Header.Add("maxWidth", "<integer>")
req.Header.Add("maxHeight", "<integer>")
req.Header.Add("extension", "<string>")
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))
}
If your input document has multiple pages and you want each page converted into a thumbnail, you can set the pages header equal to the number of pages in your document. After that, you’re all done — it’s just that simple!