Documentation

Extract Image Metadata with JSON Output Format

Illustration of image metadata

Metadata is a powerful tool when working with images. It can tell you the dimensions of your image, DPI, camera EXIF data, and more—and each piece of data is a signal you can use with imgix to display your images to their best advantage with the JSON Output Format (fm=json).

All of the other imgix output formats are image formats like JPEG, GIF, PNG, WebP, or automatically determined by Automatic Content Negotiation. With the new JSON Output Format, you can read information about your images in a familiar, structured format. This opens up a host of new possibilities.

Easily Retrieve Image Information

The imgix JSON Output Format will cause an image request to return a JSON document rather than an image. This payload will have the MIME type application/json and represent several different aspects of the input image. This is done by adding fm=json to your imgix URL.

Using the JSON Output Format, you can read the DPI, color depth, color profile information, pixel dimensions, EXIF data, and more from source images. By default, imgix strips all image metadata for size and privacy concerns. When using the JSON Output Format, however, it will not strip that data and will format it as the response. Here is an example output for a simple JPEG input image:

{
  "DPIHeight": 72,
  "Depth": 8,
  "ColorModel": "RGB",
  "DPIWidth": 72,
  "PixelHeight": 800,
  "PixelWidth": 532,
  "JFIF": {
    "DensityUnit": 1,
    "YDensity": 72,
    "JFIFVersion": [
      1,
      1
    ],
    "XDensity": 72
  },
  "ProfileName": "sRGB IEC61966-2.1"
}

Different input formats will include different keys in their JSON output payloads. Please experiment with your own input images to see which keys will be included in the output document. For a more complex example, please click on the image below.

flower

Using the new JSON Output Format, you can do more than just manipulate images with imgix. You can now reason about them as well. This has already led to richer applications for the customers using this feature.

Demo: Maintaining Aspect Ratios

To demonstrate some of the JSON Output Format’s capabilities, we’ve put together a CodePen to show how to maintain the aspect ratio of several user-generated photos. Without access to the metadata, we would have to download the image and store its dimensions server-side to accomplish the same result.

See the Pen JSON Output Format with imgix by imgix (@imgix) on CodePen.

In the example, by using just a few lines of JavaScript, we’re able to maintain the aspect ratios of our images without any server-side processing. We can read information about an image before we decide how we would like it to render, and use it to determine which parameters to apply.