fix: properly never upscale
This commit is contained in:
parent
db0c862440
commit
dc21d085ec
2 changed files with 31 additions and 11 deletions
|
@ -243,34 +243,54 @@ export const baseService: Omit<LocalImageService, 'transform'> = {
|
||||||
|
|
||||||
densityWidths.forEach((width, index) => {
|
densityWidths.forEach((width, index) => {
|
||||||
const maxTargetWidth = Math.min(width, maxWidth);
|
const maxTargetWidth = Math.min(width, maxWidth);
|
||||||
srcSet.push({
|
|
||||||
|
const srcSetValue = {
|
||||||
transform: {
|
transform: {
|
||||||
...options,
|
...options,
|
||||||
width: maxTargetWidth,
|
|
||||||
height: Math.round(maxTargetWidth / aspectRatio),
|
|
||||||
format: targetFormat,
|
|
||||||
},
|
},
|
||||||
descriptor: `${densityValues[index]}x`,
|
descriptor: `${densityValues[index]}x`,
|
||||||
attributes: {
|
attributes: {
|
||||||
type: `image/${targetFormat}`,
|
type: `image/${targetFormat}`,
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// Only set width and height if they are different from the original image
|
||||||
|
if (maxTargetWidth !== imageWidth) {
|
||||||
|
srcSetValue.transform.width = maxTargetWidth;
|
||||||
|
srcSetValue.transform.height = Math.round(maxTargetWidth / aspectRatio);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetFormat !== options.format) {
|
||||||
|
srcSetValue.transform.format = targetFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
srcSet.push();
|
||||||
});
|
});
|
||||||
} else if (widths) {
|
} else if (widths) {
|
||||||
widths.forEach((width) => {
|
widths.forEach((width) => {
|
||||||
const maxTargetWidth = Math.min(width, maxWidth);
|
const maxTargetWidth = Math.min(width, maxWidth);
|
||||||
srcSet.push({
|
|
||||||
|
const srcSetValue = {
|
||||||
transform: {
|
transform: {
|
||||||
...options,
|
...options,
|
||||||
width,
|
|
||||||
height: Math.round(maxTargetWidth / aspectRatio),
|
|
||||||
format: targetFormat,
|
|
||||||
},
|
},
|
||||||
descriptor: `${width}w`,
|
descriptor: `${width}w`,
|
||||||
attributes: {
|
attributes: {
|
||||||
type: `image/${targetFormat}`,
|
type: `image/${targetFormat}`,
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
|
||||||
|
// Only set width and height if they are different from the original image
|
||||||
|
if (maxTargetWidth !== imageWidth) {
|
||||||
|
srcSetValue.transform.width = maxTargetWidth;
|
||||||
|
srcSetValue.transform.height = Math.round(maxTargetWidth / aspectRatio);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetFormat !== options.format) {
|
||||||
|
srcSetValue.transform.format = targetFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
srcSet.push(srcSetValue);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ export function propsToFilename(transform: ImageTransform, hash: string) {
|
||||||
|
|
||||||
export function hashTransform(transform: ImageTransform, imageService: string) {
|
export function hashTransform(transform: ImageTransform, imageService: string) {
|
||||||
// Extract the fields we want to hash
|
// Extract the fields we want to hash
|
||||||
const { alt, class: className, style, ...rest } = transform;
|
const { alt, class: className, style, widths, densities, ...rest } = transform;
|
||||||
const hashFields = { ...rest, imageService };
|
const hashFields = { ...rest, imageService };
|
||||||
return shorthash(JSON.stringify(hashFields));
|
return shorthash(JSON.stringify(hashFields));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue