Files
klz-cables.com/lib/imgproxy.ts
Marc Mintel 2a47d22e26
All checks were successful
Build & Deploy / 🔍 Prepare (push) Successful in 8s
Build & Deploy / 🧪 QA (push) Successful in 1m56s
Build & Deploy / 🏗️ Build (push) Successful in 4m5s
Build & Deploy / 🚀 Deploy (push) Successful in 1m19s
Build & Deploy / 🧪 Smoke Test (push) Successful in 48s
Build & Deploy / 🛡️ Quality Gates (push) Successful in 1m39s
Build & Deploy / ⚡ Lighthouse (push) Successful in 5m51s
Build & Deploy / ♿ WCAG (push) Successful in 7m0s
Build & Deploy / 🔔 Notify (push) Successful in 2s
feat: change img proxy configuration
2026-02-23 01:40:13 +01:00

60 lines
2.0 KiB
TypeScript

/**
* 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()}`;
}