/** * Generates an imgproxy URL for a given source image and options. * * Documentation: https://docs.imgproxy.net/usage/processing */ interface ImgproxyOptions { width?: number; height?: number; resizing_type?: 'fit' | 'fill' | 'fill-down' | 'force' | 'auto'; gravity?: string; enlarge?: boolean; extension?: string; } export function getImgproxyUrl(src: string, options: ImgproxyOptions = {}): string { // Use local proxy path which is rewritten in next.config.mjs const baseUrl = '/_img'; // Handle local paths or relative URLs let absoluteSrc = src; if (src.startsWith('/')) { const baseUrlForSrc = process.env.NEXT_PUBLIC_BASE_URL || (typeof window !== 'undefined' ? window.location.origin : 'https://klz-cables.com'); if (baseUrlForSrc) { absoluteSrc = `${baseUrlForSrc.replace(/\/$/, '')}${src}`; } } // Development mapping: Map local domains to internal Docker hostnames // so imgproxy can fetch images without SSL issues or external routing if (process.env.NODE_ENV === 'development') { if (absoluteSrc.includes('klz.localhost')) { absoluteSrc = absoluteSrc.replace(/^https?:\/\/klz\.localhost/, 'http://klz-app:3000'); } else if (absoluteSrc.includes('cms.klz.localhost')) { absoluteSrc = absoluteSrc.replace(/^https?:\/\/cms\.klz\.localhost/, 'http://klz-cms:8055'); } // Also handle direct container names if needed } const { width = 0, height = 0, enlarge = false, extension = '' } = options; let quality = 80; if (extension) quality = 90; // Re-map imgproxy URL to our new parameter structure // e.g. /process?url=...&w=...&h=...&q=...&format=... const queryParams = new URLSearchParams({ url: absoluteSrc, }); if (width > 0) queryParams.set('w', width.toString()); if (height > 0) queryParams.set('h', height.toString()); if (extension) queryParams.set('format', extension.replace('.', '')); if (quality) queryParams.set('q', quality.toString()); return `${baseUrl}/process?${queryParams.toString()}`; }