nuxt / image

Plug-and-play image optimization for Nuxt applications.
https://image.nuxt.com
MIT License
1.34k stars 270 forks source link

support Cloudflare images #427

Open danielroe opened 3 years ago

danielroe commented 3 years ago

https://www.cloudflare.com/en-gb/products/cloudflare-images/

pi0 commented 3 years ago

Check out https://github.com/nuxt/image/pull/359 for context, and product docs. It is a hosted (by upload) API while there is another (experimental?) cdn-cgi/ endpoint that allows fetching remote images to work zero-config similar to netlify and vercel.

/cc @xtuc Do you have any updates?

xtuc commented 3 years ago

There's a (subtle) difference between Cloudflare Images and Image resizing (cdn-cgi/image/.. which isn't experimental);

pi0 commented 3 years ago

Thanks for clarifying @xtuc ❤️ Indeed second one is what we ideally want and is already possible for other providers. If it is out of beta now, do you have a link to docs?

xtuc commented 3 years ago

It's out of beta since September! The documentation is https://developers.cloudflare.com/images, I believe you'll need to pay but if you can send me your account details at sven @ cloudflare com, i'll ask to enable.

chk613 commented 2 years ago

the beta only gives instrucitons for next, how can we adapt to Nuxt? https://developers.cloudflare.com/images/image-resizing/integration-with-frameworks

lauragift21 commented 2 years ago

Circling back on this! I'm happy to look into this internally and see where I can help get this to work. cc @pi0 @danielroe

danielroe commented 2 years ago

Amazing - thank you @lauragift21!

theolavaux commented 2 years ago

I started using NuxtImage with Cloudinary but regarding the number of users we have it's ridiculously pricy, we would like to switch to Cloudflare Images in the near future, any ETA on this?

realandrew commented 1 year ago

Is there an ETA on this?

MickL commented 5 months ago

I am also very much interested in this. It shouldnt be too hard to integrated or am I missing something?

MickL commented 5 months ago

So I wrote a custom provider which fulfills all the points above BUT I dont know how to get the variants in. It seems like we need to have <NuxtImg> support setting variants per screen size, e.g.:

<NuxtImg variants="public xl:large">

I tried by using sizes prop but it refuses to render the image then:

<NuxtImg sizes="public xl:large">

Here is the custom provider, it works with flexible variants so far:

nuxt-config:

image: {
    provider: 'cloudflare-images',
    providers: {
      cloudflareImages: {
        name: 'cloudflare-images',
        provider: '~/providers/cloudflare-images.ts',
        options: {
          // baseURL: 'https://www.example.com',
          accountHash: '...',
        },
      },
    },
  },

/providers/cloudflare-images.ts:

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

const operationsGenerator = createOperationsGenerator({
  keyMap: {
    width: 'w',
    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 = {};

export const getImage: ProviderGetImage = (
  imageId,
  { modifiers = {}, baseURL, accountHash, variant } = {},
  ctx,
) => {
  const mergeModifiers = { ...defaultModifiers, ...modifiers };
  const operations = operationsGenerator(mergeModifiers as any);

  if (baseURL) {
    baseURL += '/cdn-cgi/imagedelivery/';
  } else {
    baseURL = 'https://imagedelivery.net/';
  }

  return {
    url: variant
      ? joinURL(baseURL, accountHash, imageId, variant)
      : joinURL(baseURL, accountHash, imageId, operations),
  };
};

Maybe someone can pick up and make a PR that also works with variants?