mirror of
https://github.com/KuekHaoYang/KVideo.git
synced 2026-08-15 00:33:44 +08:00
32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
/**
|
|
* Utility to optimize images using images.weserv.nl
|
|
* This service caches images and converts them to WebP, improving load times
|
|
* and bypassing some referrer-based blocks.
|
|
*/
|
|
export function getOptimizedImageUrl(url: string, width?: number, quality: number = 80): string {
|
|
if (!url) return '';
|
|
|
|
// Skip optimization for local images or already optimized images
|
|
if (url.startsWith('/') || url.includes('weserv.nl')) {
|
|
return url;
|
|
}
|
|
|
|
// Remove protocol for weserv usage
|
|
const cleanUrl = url.replace(/^https?:\/\//, '');
|
|
|
|
// Construct weserv URL
|
|
// url: source url (without protocol)
|
|
// w: width
|
|
// q: quality
|
|
// output: webp (for better compression)
|
|
// l: load (lazy load, though handled by next/image)
|
|
// n: no-redirect (returns error image instead of redirecting)
|
|
const baseUrl = `https://images.weserv.nl/?url=${encodeURIComponent(cleanUrl)}&output=webp&q=${quality}&n=-1`;
|
|
|
|
if (width) {
|
|
return `${baseUrl}&w=${width}`;
|
|
}
|
|
|
|
return baseUrl;
|
|
}
|