question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[source-drupal] Add ability to use image derivatives

See original GitHub issue

Summary

Drupal have a nicely developed feature to generate “image styles”: derivatives of original image differing in size, crop, etc. There are many modules in Drupal ecosystem that can extend “image styles”, e.g. Focal point.

Backend requirements:

  • Install Consumer Image Styles module.
  • Create at least one consumer and select image styles you want to get with request.
  • Add consumerId=YOUR_CONSUMER_ID query string parameter to your request.

Basic example

Your JSON:API response will contain links to selected derivatives:

"meta": {
    "imageDerivatives": {
      "links": {
        "medium: {
          "href": "http://example.org/sites/…/medium/…/img.png?itok=LQM1dnmK",
          "meta": {
            "rel": [
                "drupal://jsonapi/extensions/consumer_image_styles/links/relation-types/#derivative"
            ]
          }
        },
      }
    }
  }

Motivation

If content is managed by Drupal It would be great to use those derivatives in statically generated site. Gridsome g-image component can resize and crop local images only, and it can’t crop image around focal point. It is just one case that Drupal backend can handle for you, there are many more.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
matt-e-kingcommented, Apr 11, 2019

Hey @Al-Rozhkov! Sorry for the delay, I didn’t realize I had a another ticket assigned to me 😃. Let me investigate this further to see if I have any “knowledge-gaps” on how this could all work together.

I’ll keep you updated!

0reactions
felubracommented, Dec 23, 2019

Perhaps a more scalable solution would involve a new option in the DrupalSource, ‘fetchImageDerivatives’:

(PoC)

async buildGraphQl () {
    const options = this.createContentTypeOptions()
    const { addContentType, addCollection = addContentType } = this.actions

    this.collection = addCollection(options)

    for (const item of this.response) {
      const fields = this.processFields(item.attributes)
      const relationships = this.processRelationships(item.relationships)
      const origin = typeof item.links.self === 'object'
        ? item.links.self.href
        : item.links.self
+++   const imageDerivatives = options.fetchImageDerivatives && 
        Object.entries(item.links).reduce(
          (derivatives, [name, { href, meta }]) => { 
            if (href && meta && meta.rel && meta.rel.includes("drupal://jsonapi/extensions/consumer_image_styles/links/relation-types/#derivative")) { 
              derivatives[name] = {href}
            }; 
            return derivatives; 
          }, {})

      this.collection.addNode({
        title: fields.title || fields.name,
        date: fields.created || fields.changed,
        ...fields,
        ...relationships,
        imageDerivatives,
        id: item.id,
        internal: { origin }
      })
    }
  }

createContentTypeOptions (override = {}) {
    const {
      options: {
        routes = {},
+++     fetchImageDerivatives = false,
      } = {}
    } = this.source

    return Object.assign({
+++   fetchImageDerivatives,
      typeName: this.typeName,
      route: routes[this.entityType]
    }, override)
  }

With this new option enabled, the script would check for image derivatives inside the link attribute by looking for a item with href and the rel attribute used by the Drupal Consumer Image Styles module.

Page query:

query Articles {
  allDrupalNodeArticle {
    edges {
      node {
        id,
        field_image {
          node {
            imageDerivatives {
              large {
                href
              },
              custom_image_style {
                  href
              },
            },
            uri {
              url
            }
          }
        }
      }
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Speed up Gatsby Builds With Drupal Image Processing
Drupal has the built in ability to generate its own derivative images on demand and cache them, so why not just build our...
Read more >
How to use Drupal to optimize images on the fly - Desarol
It allows developers to take full advantage of Drupal's image styles and ImageToolkit abstraction to generate derivatives on the fly using ...
Read more >
image.module - Convert theme_ functions to Twig - Drupal
Add a new image style. Go to Configuration->Image Styles, click on the 'Add image style' button, and enter an image style name (let's...
Read more >
Image Derivative - Chris McCormick
One simple example is that you can take the derivative in the x-direction at pixel x1 by taking the difference between the pixel...
Read more >
Libraries - @imgix/gatsby
Overview / Resources; Why use imgix instead of gatsby-transform-sharp? ... As we are able to create far more derivative images at different resolutions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found