@adonis-agora/media
Version:
Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a
109 lines • 4.54 kB
JavaScript
import { placeholderOf } from './transformers/placeholder.js';
/** Formats every current browser decodes; anything else (HEIC, TIFF) is never a candidate. */
export const BROWSER_IMAGE_TYPES = new Set([
'image/jpeg',
'image/png',
'image/webp',
'image/avif',
'image/gif',
]);
/**
* Pick the `srcset` candidates of a record: the generated presets that are the same picture at
* another width, plus the original when it is a browser-decodable image of known width. Sorted
* narrowest first, one per width. Pure — {@link MediaLibrary.image} resolves the URLs.
*
* "The same picture" means the same aspect ratio as a reference: the original's, or — when the
* caller lists `include` explicitly, which is how an art-directed `<source>` (square crops for
* phones, say) asks for its group — the first listed conversion's. The original joins only if it
* matches that reference.
*
* @internal exported for tests.
*/
export function imageCandidates(record, presets, include) {
const placeholder = placeholderOf(record);
const originalAspect = placeholder ? placeholder.width / placeholder.height : undefined;
const sized = [];
for (const preset of presets) {
if (include && !include.includes(preset.name))
continue;
if (preset.blur)
continue;
const conversion = record.conversions[preset.name];
if (!conversion?.path)
continue;
const size = conversionSize(preset, conversion.meta);
if (!size)
continue;
const aspect = size.height !== undefined
? size.width / size.height
: size.keepsAspect
? originalAspect
: undefined;
sized.push({
name: preset.name,
width: size.width,
height: size.height,
aspect,
keepsAspect: size.keepsAspect,
});
}
let reference = originalAspect;
if (include) {
const first = include
.map((name) => sized.find((s) => s.name === name))
.find((s) => s?.aspect !== undefined);
if (first)
reference = first.aspect;
}
const candidates = [];
for (const s of sized) {
const fits = reference === undefined
? s.keepsAspect
: s.aspect !== undefined && sameAspect(s.aspect, reference);
if (!fits)
continue;
const height = s.height ?? (s.aspect ? Math.round(s.width / s.aspect) : undefined);
candidates.push({ conversion: s.name, width: s.width, ...(height ? { height } : {}) });
}
if (placeholder &&
BROWSER_IMAGE_TYPES.has(record.mimeType) &&
(reference === undefined || sameAspect(originalAspect, reference))) {
candidates.push({
conversion: undefined,
width: placeholder.width,
height: placeholder.height,
});
}
candidates.sort((a, b) => a.width - b.width);
return candidates.filter((c, i) => i === 0 || c.width !== candidates[i - 1].width);
}
/**
* Output size of a generated preset. The processor records it in `meta` (since 0.22); for a
* conversion generated before that, it is derived from the preset when the preset pins it down:
* a lone `width` keeps the aspect ratio, and `cover`/`fill`/`contain` output exactly
* `width`×`height`. `keepsAspect` says whether the preset can change the aspect ratio at all —
* only one that can (`width` AND `height`, cropped or padded) needs the comparison.
*/
function conversionSize(preset, meta) {
const fit = preset.fit ?? 'cover';
const keepsAspect = !(preset.width && preset.height) || fit === 'inside' || fit === 'outside';
if (meta && isDimension(meta.width) && isDimension(meta.height)) {
return { width: meta.width, height: meta.height, keepsAspect };
}
if (!preset.width)
return undefined;
if (!preset.height)
return { width: preset.width, keepsAspect: true };
if (fit === 'cover' || fit === 'fill' || fit === 'contain') {
return { width: preset.width, height: preset.height, keepsAspect: false };
}
return undefined;
}
function isDimension(value) {
return typeof value === 'number' && Number.isFinite(value) && value > 0;
}
/** Same picture at another size? Whole-pixel rounding skews tiny outputs, hence the tolerance. */
function sameAspect(candidate, reference) {
return Math.abs(candidate - reference) / reference < 0.02;
}
//# sourceMappingURL=responsive_image.js.map