gpt-4-0314· openai
Suzhen/CodeChat
Please create nuxt-image provider for imgproxy.net. Use Nuxt 3
- User
Please create nuxt-image provider for imgproxy.net. Use Nuxt 3
- Assistant
Since Nuxt 3 is still in beta, the following example is based on Nuxt 2 alongside the Nuxt Image module, and it demonstrates creating a custom provider for Imgproxy.net.
First, install the required dependencies in your Nuxt.js project:
npm install @nuxt/imageNext, create a new plugin file called
imgproxy-provider.jsinside thepluginsdirectory:// plugins/imgproxy-provider.js import { createOperationsGenerator } from '@nuxt/image/dist/runtime/providers/utils' import { joinURL, encodePath } from 'ufo' const operationsGenerator = createOperationsGenerator({ key: 'format', valueMap: { 'avif': 'f:avif', 'jpeg': 'f:jpeg', 'jpg': 'f:jpg', 'png': 'f:png', 'svg': 'f:svg', 'webp': 'f:webp', } }) export default function (provider, options) { provider.provider = { getImage: (src, { modifiers = {}, baseURL = options.baseURL } = {}) => { const operations = operationsGenerator(modifiers) const format = (operations.match(/f:(\w+)/) || [])[1] const path = [options.salt, src, operations].join('/') const encodedPath = encodePath(path) return { url: joinURL(baseURL, encodedPath), format, } }, } }