fix: properly never upscale

This commit is contained in:
Princesseuh 2023-10-06 18:45:22 +02:00
parent db0c862440
commit dc21d085ec
No known key found for this signature in database
GPG key ID: 105BBD6D57F2B0C0
2 changed files with 31 additions and 11 deletions

View file

@ -243,34 +243,54 @@ export const baseService: Omit<LocalImageService, 'transform'> = {
densityWidths.forEach((width, index) => {
const maxTargetWidth = Math.min(width, maxWidth);
srcSet.push({
const srcSetValue = {
transform: {
...options,
width: maxTargetWidth,
height: Math.round(maxTargetWidth / aspectRatio),
format: targetFormat,
},
descriptor: `${densityValues[index]}x`,
attributes: {
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) {
widths.forEach((width) => {
const maxTargetWidth = Math.min(width, maxWidth);
srcSet.push({
const srcSetValue = {
transform: {
...options,
width,
height: Math.round(maxTargetWidth / aspectRatio),
format: targetFormat,
},
descriptor: `${width}w`,
attributes: {
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);
});
}

View file

@ -17,7 +17,7 @@ export function propsToFilename(transform: ImageTransform, hash: string) {
export function hashTransform(transform: ImageTransform, imageService: string) {
// 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 };
return shorthash(JSON.stringify(hashFields));
}