@imgix/contentful

Browse for, select, and insert image assets into your content quickly and easily via the imgix Image Manager.
TypeScript 5 6 days ago imgix/contentful

Installation

The app can be installed to your Contentful workspace via the marketplace.

If running locally, the app can be installed via npm:

npm install @imgix/contentful
npm run start

Configuration

Upon installation, configure the app using an API key generated via the imgix Dashboard. Ensure that the generated key has the following permissions: Sources and Image Manager Browse.

Following the instructions on the screen, enter in the API key and press Verify. If the key is valid, you will receive a notification that the key has been successfully verified. If verification fails, you will need to ensure that the key was entered correctly.

Assign to Fields (Optional)

The configuration page surfaces the option for users to select pre-existing content fields that are compatible with the imgix app. Note that the app is configured to integrate with JSON object fields only, therefore only fields of this type will be displayed. Users may prefer this method over selecting individual fields manually for each applicable content model.

Add to Content Model

Of the many content types that users can choose from, imgix specifically integrates with the JSON object. Please note that if you are currently using a Media content type for images, you will need to create a new field of type JSON object to integrate the app with. Designate a field to use imgix on by navigating to that field’s Appearance tab and selecting the app. This step can be skipped if you already assigned the app directly to the desired field(s).

Browse and Select Images

From any instance of a field using the imgix app, a modal can be opened to browse images by imgix source. First, select a desired source to browse images from. Using any of the pagination buttons, navigate each page of assets to choose from. After selecting an image, it can be inserted to the field via the Add image button. Additionally, there are options to replace an image, or clear a selection from the field altogether.

Query an Image

Once the content is published, developers can query the src of the selected image, returned as a string, via the Contentful API. The example below demonstrates this using GraphQL, but this can be done independent of any specific tool.

query MyQuery {
  allContentfulArticle {
    nodes {
      avatar {
        src
      }
      bannerImage {
        src
      }
    }
  }
}

returns something similar to:

{
  "data": {
    "allContentfulArticle": {
      "nodes": [
        {
          "avatar": {
            "src": "https://fourbottle.imgix.net/heroes/pourover.jpg"
          },
          "bannerImage": {
            "src": "https://fourbottle.imgix.net/heroes/light-scatter.jpg"
          }
        }
      ]
    }
  },
  "extensions": {}
}

Transforming Images

Developers can leverage the power of imgix's rendering API downstream from where the image was selected in Contentful. We recommend piping the value of the src field of the image through to one of imgix's SDKs. The example below builds on the previous one by passing the image src through to @imgix/gatsby component:

import React from "react";
import { graphql } from "gatsby";
import { ImgixGatsbyImage } from "@imgix/gatsby";

export default function Page({ data }) {
  return (
    data.allContentfulArticle.nodes.map(({ node }) => (
        <ImgixGatsbyImage
          src={node.avatar.src}
          imgixParams={{
            auto: "format,compress",
            crop: "faces,edges",
          }}
          layout="constrained"
          width={350}
          aspectRatio={16 / 9}
          sizes="(min-width: 1024px) calc(30vw - 128px), (min-width: 768px) calc(50vw - 100px), calc(100vw - 70px)"
          alt="An imgix-served image from Contentful"
        />
    ))
  );
}

export const query = graphql`
query MyQuery {
  allContentfulArticle {
    nodes {
      avatar {
        src
      }
    }
  }
}
`;

License

FOSSA Status


More Plugins