Return Image Metadata (including EXIF and Resolution) using Go

Cloudmersive
2 min readMar 22, 2022

Image files tend to contain a lot more than just pixels. With our Image Metadata API, you can input an image and easily retrieve information about that image’s file type, resolution, and even EXIF data (if relevant/available). To include this API in your Go project, take advantage of ready-to-run code below and simply copy & paste to your environment:

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/image/get-info/metadata"
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))
}

Take a look at a sample JSON response for reference:

{
"Successful": true,
"IsValidImage": true,
"FileFormat": "string",
"Width": 0,
"Height": 0,
"BitDepth": 0,
"HasTransparency": true,
"ColorSpace": "string",
"ExifProfileName": "string",
"ExifValues": [
{
"Tag": "string",
"DataType": "string",
"DataValue": "string"
}
]
}

If this is your first time using a Cloudmersive API, you’ll need to head to our website (www.cloudmersive.com) to create an account and receive an API key. You can choose from several account options, including a free-tier account with a limit of 800 API calls per month. A single account will provide access to dozens of useful APIs via the Cloudmersive API Console.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.