Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudlfare Images doesn't work #891

Closed
NeroN7F opened this issue Jun 24, 2023 · 4 comments
Closed

Cloudlfare Images doesn't work #891

NeroN7F opened this issue Jun 24, 2023 · 4 comments
Labels
bug Something isn't working provider

Comments

@NeroN7F
Copy link

NeroN7F commented Jun 24, 2023

Is there a way to use cloudflare provider with Cloudflare Images? Seems like it makes the image request to the

https://imagedelivery.net/<my-id>/cdn-cgi/image/w=1536,f=webp/ ,

which is the format of the Cloudflare Image Resizing, while the actual Cloudflare Images using url like below

https://imagedelivery.net/<my-id>/<img-id>/public

The custom provider solves the problem, however you are losing the features of the Nuxt-Picture srcset attributes, for the different screen sizes

@danielroe danielroe added bug Something isn't working provider labels Jun 24, 2023 — with Volta.net
@Hiratake
Copy link

Hiratake commented Jul 2, 2023

I am in the same situation. Setting cloudflare as the provider does not work with Cloudflare Images.

Are there any plans to support Cloudflare Images in Nuxt Image?
The output URLs appear to support Cloudflare Image Resizing, not Cloudflare Images.

If you support Cloudflare Images, the Cloudflare documentation indicates that the URL should look like this

https://example.com/cdn-cgi/imagedelivery/<ACCOUNT_HASH>/<IMAGE_ID>/<VARIANT_NAME>

The /cdn-cgi/image/ must be changed to /cdn-cgi/imagedelivery/.

Also, the location of the parameters seems to be different.

When using flexible variations, it appears that the parameters must be given after the <IMAGE_ID>, but currently they are given before.

@NeroN7F
Copy link
Author

NeroN7F commented Jul 11, 2023

As a workaround solution for now, custom provider works fine, which allows to use nuxt-picture and CloudFlare Images, preserving the image optimization

import { joinURL } from 'ufo'

export function getImage(src, { modifiers, baseURL } = {}, { options, $img }) {

const { width, height, format, fit, quality, ...providerModifiers } = modifiers
const operations = [`quality=${quality}`, `width=${width}`, `height=${height}`, `format=${format}`, `fit=${fit}`]

const operationsString = operations.join(',')
return {
    url: joinURL(baseURL, src, operationsString)
}

}

@velidonmez
Copy link

I customized cloudflare provider like below and implemented it as custom provider, it solved my problem.

import { encodeQueryItem, joinURL } from 'ufo'
import type { ProviderGetImage } from '@nuxt/image'
import { createOperationsGenerator } from '#image'

const operationsGenerator = createOperationsGenerator({
  keyMap: {
    width: 'w',
    blur: 'blur',
    height: 'h',
    dpr: 'dpr',
    fit: 'fit',
    gravity: 'g',
    quality: 'q',
    format: 'f',
    sharpen: 'sharpen',
  },
  valueMap: {
    fit: {
      cover: 'cover',
      contain: 'contain',
      fill: 'scale-down',
      outside: 'crop',
      inside: 'pad',
    },
    gravity: {
      auto: 'auto',
      side: 'side',
    },
  },
  joinWith: ',',
  formatter: (key, val) => encodeQueryItem(key, val),
})

const defaultModifiers = {}

// https://developers.cloudflare.com/images/image-resizing/url-format/
export const getImage: ProviderGetImage = (src, {
  modifiers = {},
  baseURL = '/',
} = {}) => {
  const mergeModifiers = { ...defaultModifiers, ...modifiers }
  const operations = operationsGenerator(mergeModifiers as any)
  // https://example.com/cdn-cgi/imagedelivery/<ACCOUNT_HASH>/<IMAGE_ID>/<VARIANT_NAME>
  const url = operations ? joinURL(baseURL, src, operations) : joinURL(baseURL, src, 'public')

  return {
    url,
  }
}
<div>
    <NuxtImg
      provider="customProvider"
      src="/637c5bb6-6842-4cc5-ca9b-0f57715e3000"
      width="600"
    />
    <NuxtImg
      quality="50"
      provider="customProvider"
      src="/637c5bb6-6842-4cc5-ca9b-0f57715e3000"
      width="400"
      :modifiers="{ blur: '50' }"
    />
    <NuxtImg
      quality="50"
      provider="customProvider"
      src="/637c5bb6-6842-4cc5-ca9b-0f57715e3000"
      width="200"
      height="200"
    />
  </div>

image

@Baroshem
Copy link
Collaborator

Closing ad the workaround was provided.

@Baroshem Baroshem closed this as not planned Won't fix, can't repro, duplicate, stale Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working provider
Projects
None yet
Development

No branches or pull requests

5 participants