Unable to use focalpoint for images

I am trying to implement focalpoints to images in my Next.js project. But I believe that focal points are not actually retrieved from DatoCMS.

This is how the focalpoint has been set on the media item:


According to the documentation of imgix, I should be able to retrieve the focalpoint from an url like this: https://www.datocms-assets.com/29954/1602837432-kvinde-der-gar-ved-siden-af-bil.jpg?crop=focalpoint&fit=crop&fp-debug=true&h=400&w=700

But for some reason the focalpoint is in the center of the image.

I can see that imgix seems to work fine with other parameters like these:

https://www.datocms-assets.com/29954/1602837432-kvinde-der-gar-ved-siden-af-bil.jpg?fit=facearea&facepad=4&h=400&w=700

So as far as I am concerned, the focalpoint data is not retrieved correctly from DatoCMS. Could that be? Is there a server cache I can reset or something like this? Or is it just a bug in DatoCMS?

Hi @online,

Hmm… I’m noticing some strange behavior here and I’m not sure if it’s a bug. I’ll check with the devs on that. I’ll explain what the possible bug is in a sec.

But first, just in case it wasn’t clear, the focal point is actually a separate parameter you have to retrieve with the CDA and then add to the Imgix URL query. Imgix is a CDN that doesn’t modify the original source images, so we don’t have a way to “save” that focal point information to the file permanently.

Their other documentation is clearer about this: https://docs.imgix.com/en-US/getting-started/tutorials/cropping-and-enhancement/focal-point-cropping (emphasis added)

Using the focal point parameters, we can adjust his position vertically. First we set the focal point, using the fp-debug parameter to show the coordinates and adjust until he is vertically at the center

The fp-debug parameter is meant to help you see what your fp-x and fp-y values are pointing to as you adjust them. The image itself doesn’t contain those values. Imgix itself only takes the x and y values from the URL parameters; the “click to set focal point” feature is a separate feature we added to DatoCMS.

In our system, that focal point data lives in a separate metadata layer that we store alongside your image (like with its title, caption, extra info, etc.) Then we send that to Imgix, in the form of URL params, on an as-needed basis. You can always explicitly ask for the raw focal point values from us by asking the GraphQL CDA for the focalPoint property of the asset, like this:

query MyQuery {
  upload(filter: {id: {eq: "YOUR_IMAGE_ID"}}) {
    responsiveImage(imgixParams: {fpDebug: "true"}) {
      src
    }
    focalPoint {
      x
      y
    }
  }
}

The resulting response will include:

"focalPoint": {
        "x": 0.6,
        "y": 0.24
}

Which you can then append to the Imgix URL, like https://www.datocms-assets.com/29954/1602837432-kvinde-der-gar-ved-siden-af-bil.jpg?fit=crop&fp-debug=true&fp-x=0.6&fp-y=0.24

So far so good, I hope?

What’s strange is that we actually automatically add the fp-x and fp-y params for you, but only if you explicitly provide an image width and height and force fit=crop:

But I don’t see why that should be necessary, since https://www.datocms-assets.com/29954/1602837432-kvinde-der-gar-ved-siden-af-bil.jpg?fp-debug=true&fp-x=0.6&fp-y=0.24 works fine even without the explicit w, h, and force=crop. So that part might be a bug, or it might just be an optimization we do (why send the focal point at all if we’re not doing any cropping). I’ll double-check with the devs for you.

In the meantime, you can either specify those additional parameters (w, h, and fit=crop) or just manually append the x and y values from the focalPoint GraphQL property. Sorry for the extra hassle!

I’ll let you know what the devs say.

@online,

We shipped a small improvement to this. Now if you request fpDebug: true in GraphQL, we also automatically append the fp-x and fp-y parameters to the src URL.

Please note that the other things I said above are still relevant, namely:

  • The focal point is not saved into the image itself, but rather lives in a separate metadata layer
  • You can explicitly query for it by asking GraphQL for the focalPoint property
  • Or it will be automatically added to the URL if you make a GraphQL query with either 1) fpDebug set (new behavior) OR 2) fit=crop, w, and h all set (existing behavior)
  • However you get it, the end URL to Imgix must include the fp-x and fp-y params or nothing will happen ← this is really the thing to make sure about.
    • And on the Imgix side, the focal point is only used if you crop the image a certain way (usually fit=crop) and/or specify fp-debug=true. If you don’t have either of those, setting the focal point doesn’t do anything by itself (which is why we normally don’t send it unless you ask for those)

Hope that helps, but please let me know if anything is unclear or still not working!