imagetools-core
Advanced tools
Comparing version 4.1.0 to 5.0.0
@@ -17,3 +17,3 @@ import { createHash } from 'node:crypto'; | ||
return; | ||
setMetadata(image, 'background', background); | ||
image[METADATA].backgroundDirective = background; | ||
return background; | ||
@@ -30,3 +30,3 @@ }; | ||
return function blurTransform(image) { | ||
setMetadata(image, 'blur', blur); | ||
image[METADATA].blur = blur; | ||
return image.blur(blur); | ||
@@ -47,3 +47,3 @@ }; | ||
return; | ||
setMetadata(image, 'fit', fit); | ||
image[METADATA].fit = fit; | ||
return fit; | ||
@@ -56,3 +56,3 @@ }; | ||
return function flattenTransform(image) { | ||
setMetadata(image, 'flatten', true); | ||
image[METADATA].flatten = true; | ||
return image.flatten({ | ||
@@ -68,3 +68,3 @@ background: getBackground(config, image) | ||
return function flipTransform(image) { | ||
setMetadata(image, 'flip', true); | ||
image[METADATA].flip = true; | ||
return image.flip(); | ||
@@ -78,3 +78,3 @@ }; | ||
return function flopTransform(image) { | ||
setMetadata(image, 'flop', true); | ||
image[METADATA].flop = true; | ||
return image.flop(); | ||
@@ -88,3 +88,3 @@ }; | ||
return; | ||
setMetadata(image, 'quality', quality); | ||
image[METADATA].quality = quality; | ||
return quality; | ||
@@ -96,3 +96,3 @@ }; | ||
return; | ||
setMetadata(image, 'progressive', true); | ||
image[METADATA].progressive = true; | ||
return true; | ||
@@ -104,8 +104,8 @@ }; | ||
return; | ||
setMetadata(image, 'lossless', true); | ||
image[METADATA].lossless = true; | ||
return true; | ||
}; | ||
const formatValues = ['avif', 'jpg', 'jpeg', 'png', 'heif', 'heic', 'webp', 'tiff']; | ||
const format$1 = (config) => { | ||
const formatValues = ['avif', 'jpg', 'jpeg', 'png', 'heif', 'webp', 'tiff']; | ||
const format = (config) => { | ||
let format = undefined; | ||
@@ -120,3 +120,3 @@ if (config.format && formatValues.includes(config.format)) { | ||
return function formatTransform(image) { | ||
setMetadata(image, 'format', format); | ||
image[METADATA].format = format; | ||
return image.toFormat(fixedFormat, { | ||
@@ -134,3 +134,3 @@ quality: getQuality(config, image), | ||
return function grayscaleTransform(image) { | ||
setMetadata(image, 'grayscale', true); | ||
image[METADATA].grayscale = true; | ||
return image.grayscale(); | ||
@@ -147,5 +147,5 @@ }; | ||
return function hsbTransform(image) { | ||
setMetadata(image, 'hue', hue); | ||
setMetadata(image, 'saturation', saturation); | ||
setMetadata(image, 'brightness', brightness); | ||
image[METADATA].hue = hue; | ||
image[METADATA].saturation = saturation; | ||
image[METADATA].brightness = brightness; | ||
return image.modulate({ | ||
@@ -163,3 +163,3 @@ hue: hue || 0, | ||
return function invertTransform(image) { | ||
setMetadata(image, 'invert', true); | ||
image[METADATA].invert = true; | ||
return image.negate(); | ||
@@ -172,3 +172,3 @@ }; | ||
if (kernel && kernelValues.includes(kernel)) { | ||
setMetadata(image, 'kernel', kernel); | ||
image[METADATA].kernel = kernel; | ||
return kernel; | ||
@@ -183,3 +183,3 @@ } | ||
return function medianTransform(image) { | ||
setMetadata(image, 'median', median); | ||
image[METADATA].median = median; | ||
return image.median(median); | ||
@@ -193,3 +193,3 @@ }; | ||
return function normalizeTransform(image) { | ||
setMetadata(image, 'normalize', true); | ||
image[METADATA].normalize = true; | ||
return image.normalize(); | ||
@@ -241,3 +241,3 @@ }; | ||
return; | ||
setMetadata(image, 'position', position); | ||
image[METADATA].position = position; | ||
return position; | ||
@@ -275,2 +275,3 @@ }; | ||
const allowUpscale = config.allowUpscale === '' || config.allowUpscale === 'true'; | ||
const basePixels = parseInt(config.basePixels || ''); | ||
if (!width && !height && !aspect) | ||
@@ -281,4 +282,4 @@ return; | ||
// calculate finalWidth & finalHeight | ||
const originalWidth = getMetadata(image, 'width'); | ||
const originalHeight = getMetadata(image, 'height'); | ||
const originalWidth = image[METADATA].width; | ||
const originalHeight = image[METADATA].height; | ||
const originalAspect = originalWidth / originalHeight; | ||
@@ -339,6 +340,7 @@ let finalWidth = width, finalHeight = height, finalAspect = aspect; | ||
finalHeight = Math.round(finalHeight); | ||
setMetadata(image, 'height', finalHeight); | ||
setMetadata(image, 'width', finalWidth); | ||
setMetadata(image, 'aspect', finalAspect); | ||
setMetadata(image, 'allowUpscale', allowUpscale); | ||
image[METADATA].height = finalHeight; | ||
image[METADATA].width = finalWidth; | ||
image[METADATA].aspect = finalAspect; | ||
image[METADATA].allowUpscale = allowUpscale; | ||
image[METADATA].pixelDensityDescriptor = basePixels > 0 ? finalWidth / basePixels + 'x' : undefined; | ||
return image.resize({ | ||
@@ -361,3 +363,3 @@ width: finalWidth || undefined, | ||
return function rotateTransform(image) { | ||
setMetadata(image, 'rotate', rotate); | ||
image[METADATA].rotate = rotate; | ||
return image.rotate(rotate, { | ||
@@ -373,3 +375,3 @@ background: getBackground(config, image) | ||
return function tintTransform(image) { | ||
setMetadata(image, 'tint', '#' + tint); | ||
image[METADATA].tint = '#' + tint; | ||
return image.tint('#' + tint); | ||
@@ -384,3 +386,3 @@ }; | ||
flop, | ||
format$1, | ||
format, | ||
grayscale, | ||
@@ -400,18 +402,21 @@ hsb, | ||
}; | ||
const srcsetFormat = () => (metadatas) => { | ||
const sources = metadatas.map((meta) => `${meta.src} ${meta.width}w`); | ||
return sources.join(', '); | ||
}; | ||
const srcsetFormat = () => metadatasToSourceset; | ||
const metadataFormat = (whitelist) => (metadatas) => { | ||
if (whitelist) { | ||
metadatas = metadatas.map((cfg) => Object.fromEntries(Object.entries(cfg).filter(([k]) => whitelist.includes(k)))); | ||
} | ||
metadatas.forEach((m) => delete m.image); | ||
return metadatas.length === 1 ? metadatas[0] : metadatas; | ||
const result = whitelist | ||
? metadatas.map((cfg) => Object.fromEntries(Object.entries(cfg).filter(([k]) => whitelist.includes(k)))) | ||
: metadatas; | ||
result.forEach((m) => delete m.image); | ||
return result.length === 1 ? result[0] : result; | ||
}; | ||
const metadataToSource = (m) => ({ src: m.src, w: m.width }); | ||
const metadatasToSourceset = (metadatas) => metadatas | ||
.map((meta) => { | ||
const density = meta.pixelDensityDescriptor; | ||
return density ? `${meta.src} ${density}` : `${meta.src} ${meta.width}w`; | ||
}) | ||
.join(', '); | ||
/** normalizes the format for use in mime-type */ | ||
const format = (m) => m.format.replace('jpg', 'jpeg'); | ||
const sourceFormat = () => (metadatas) => { | ||
return metadatas.map((m) => metadataToSource(m)); | ||
const getFormat = (m) => { | ||
if (!m.format) | ||
throw new Error(`Could not determine image format`); | ||
return m.format.replace('jpg', 'jpeg'); | ||
}; | ||
@@ -434,6 +439,3 @@ const imgFormat = () => (metadatas) => { | ||
if (metadatas.length >= 2) { | ||
result.srcset = []; | ||
for (let i = 0; i < metadatas.length; i++) { | ||
result.srcset.push(metadataToSource(metadatas[i])); | ||
} | ||
result.srcset = metadatasToSourceset(metadatas); | ||
} | ||
@@ -444,3 +446,3 @@ return result; | ||
const pictureFormat = () => (metadatas) => { | ||
const fallbackFormat = [...new Set(metadatas.map((m) => format(m)))].pop(); | ||
const fallbackFormat = [...new Set(metadatas.map((m) => getFormat(m)))].pop(); | ||
let largestFallback; | ||
@@ -451,3 +453,3 @@ let largestFallbackSize = 0; | ||
const m = metadatas[i]; | ||
if (format(m) === fallbackFormat) { | ||
if (getFormat(m) === fallbackFormat) { | ||
fallbackFormatCount++; | ||
@@ -460,6 +462,6 @@ if (m.width > largestFallbackSize) { | ||
} | ||
const sources = {}; | ||
const sourceMetadatas = {}; | ||
for (let i = 0; i < metadatas.length; i++) { | ||
const m = metadatas[i]; | ||
const f = format(m); | ||
const f = getFormat(m); | ||
// we don't need to create a source tag for the fallback format if there is | ||
@@ -470,9 +472,13 @@ // only a single image in that format | ||
} | ||
if (sources[f]) { | ||
sources[f].push(metadataToSource(m)); | ||
if (sourceMetadatas[f]) { | ||
sourceMetadatas[f].push(m); | ||
} | ||
else { | ||
sources[f] = [metadataToSource(m)]; | ||
sourceMetadatas[f] = [m]; | ||
} | ||
} | ||
const sources = {}; | ||
for (const [key, value] of Object.entries(sourceMetadatas)) { | ||
sources[key] = metadatasToSourceset(value); | ||
} | ||
const result = { | ||
@@ -493,3 +499,2 @@ sources, | ||
url: urlFormat, | ||
source: sourceFormat, | ||
srcset: srcsetFormat, | ||
@@ -589,3 +594,3 @@ img: imgFormat, | ||
async function applyTransforms(transforms, image, removeMetadata = true) { | ||
image[METADATA] = await image.metadata(); | ||
image[METADATA] = { ...(await image.metadata()) }; | ||
if (removeMetadata) { | ||
@@ -611,3 +616,3 @@ // delete the private metadata | ||
export { applyTransforms, blur, builtinOutputFormats, builtins, extractEntries, fitValues, flatten, flip, flop, format$1 as format, formatValues, generateImageID, generateTransforms, getBackground, getFit, getKernel, getLossless, getMetadata, getPosition, getProgressive, getQuality, grayscale, hsb, imgFormat, invert, kernelValues, loadImage, median, metadataFormat, normalize, parseURL, pictureFormat, positionShorthands, positionValues, resize, resolveConfigs, rotate, setMetadata, sourceFormat, srcsetFormat, tint, urlFormat }; | ||
export { applyTransforms, blur, builtinOutputFormats, builtins, extractEntries, fitValues, flatten, flip, flop, format, formatValues, generateImageID, generateTransforms, getBackground, getFit, getKernel, getLossless, getMetadata, getPosition, getProgressive, getQuality, grayscale, hsb, imgFormat, invert, kernelValues, loadImage, median, metadataFormat, normalize, parseURL, pictureFormat, positionShorthands, positionValues, resize, resolveConfigs, rotate, setMetadata, srcsetFormat, tint, urlFormat }; | ||
//# sourceMappingURL=index.js.map |
import { Sharp } from 'sharp'; | ||
import { ImageMetadata } from '../types'; | ||
export declare const METADATA: unique symbol; | ||
declare module 'sharp' { | ||
interface Sharp { | ||
[METADATA]: Record<string, unknown>; | ||
[METADATA]: ImageMetadata; | ||
} | ||
@@ -7,0 +8,0 @@ } |
@@ -5,3 +5,2 @@ import type { OutputFormat } from './types.js'; | ||
export declare const metadataFormat: OutputFormat; | ||
export declare const sourceFormat: OutputFormat; | ||
export declare const imgFormat: OutputFormat; | ||
@@ -12,3 +11,2 @@ /** fallback format should be specified last */ | ||
url: OutputFormat; | ||
source: OutputFormat; | ||
srcset: OutputFormat; | ||
@@ -15,0 +13,0 @@ img: OutputFormat; |
import { TransformOption } from '../types.js'; | ||
export declare const fitValues: string[]; | ||
export declare const fitValues: readonly ["cover", "contain", "fill", "inside", "outside"]; | ||
export type FitValue = (typeof fitValues)[number]; | ||
@@ -4,0 +4,0 @@ export interface FitOptions { |
import { TransformFactory } from '../types.js'; | ||
export declare const formatValues: readonly ["avif", "jpg", "jpeg", "png", "heif", "heic", "webp", "tiff"]; | ||
export declare const formatValues: readonly ["avif", "jpg", "jpeg", "png", "heif", "webp", "tiff"]; | ||
export type FormatValue = (typeof formatValues)[number]; | ||
@@ -4,0 +4,0 @@ export interface FormatOptions { |
import { TransformFactory } from '../types.js'; | ||
export interface ResizeOptions { | ||
/** width in pixels */ | ||
w: string; | ||
/** height in pixels */ | ||
h: string; | ||
/** aspect ratio */ | ||
aspect: string; | ||
/** Whether to allow making images larger. This is generally a waste, so is disabled by default. */ | ||
allowUpscale: '' | 'true'; | ||
/** | ||
* The width in pixels for the 1x pixel density descriptor. | ||
* If supplied, output will use pixel density descriptors rather than width descriptors. | ||
*/ | ||
basePixels: string; | ||
} | ||
export declare const resize: TransformFactory<ResizeOptions>; |
@@ -1,3 +0,35 @@ | ||
import { Sharp } from 'sharp'; | ||
export type ImageConfig = Record<string, unknown>; | ||
import { Metadata, Sharp } from 'sharp'; | ||
import { kernelValues } from './transforms/kernel'; | ||
import { positionValues } from './transforms/position'; | ||
export interface ProcessedImageMetadata extends ImageMetadata { | ||
src: string; | ||
image: Sharp; | ||
} | ||
export interface ImageMetadata extends Metadata { | ||
allowUpscale?: boolean; | ||
aspect?: number | undefined; | ||
backgroundDirective?: string; | ||
blur?: number | boolean | undefined; | ||
brightness?: number | '' | undefined; | ||
fit?: string; | ||
flip?: true; | ||
flop?: true; | ||
flatten?: true; | ||
hue?: number | '' | undefined; | ||
invert?: true; | ||
grayscale?: true; | ||
kernel?: (typeof kernelValues)[number]; | ||
lossless?: true; | ||
median?: number; | ||
normalize?: true; | ||
pixelDensityDescriptor?: string | undefined; | ||
position?: (typeof positionValues)[number]; | ||
progressive?: true; | ||
quality?: number; | ||
saturation?: number | '' | undefined; | ||
tint?: string; | ||
rotate?: number; | ||
[key: string]: unknown; | ||
} | ||
export type ImageConfig = Record<string, string | string[]>; | ||
export interface Logger { | ||
@@ -18,3 +50,3 @@ info: (msg: string) => void; | ||
image: Sharp; | ||
metadata: Record<string, unknown>; | ||
metadata: ImageMetadata; | ||
} | ||
@@ -24,13 +56,4 @@ /** | ||
*/ | ||
export type OutputFormat = (args?: string[]) => (metadata: ImageConfig[]) => unknown; | ||
export type OutputFormat = (args?: string[]) => (metadata: ProcessedImageMetadata[]) => unknown; | ||
/** | ||
* The source output format. | ||
* Can be used to dynamically construct a sourceset string when you need to | ||
* choose between width descriptor, pixel density descriptor, or no descriptor. | ||
*/ | ||
export interface Source { | ||
src: string; | ||
w: number; | ||
} | ||
/** | ||
* The img output format. | ||
@@ -49,3 +72,3 @@ */ | ||
h: number; | ||
srcset?: Source[]; | ||
srcset?: string; | ||
} | ||
@@ -56,3 +79,6 @@ /** | ||
export interface Picture { | ||
sources: Record<string, Source[]>; | ||
/** | ||
* Key is format. Value is srcset. | ||
*/ | ||
sources: Record<string, string>; | ||
img: { | ||
@@ -59,0 +85,0 @@ src: string; |
{ | ||
"name": "imagetools-core", | ||
"version": "4.1.0", | ||
"version": "5.0.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
80454
850