Skip to content

Commit e0eee06

Browse files
authored
Add option to provide an image crop offset to fastly image resizer (#13802)
1 parent 17dd3ac commit e0eee06

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

dotcom-rendering/src/components/CardPicture.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,13 @@ const decideImageWidths = (
110110
breakpoint: breakpoints.tablet,
111111
width: 700,
112112
aspectRatio: '5:3',
113+
cropOffset: { x: 50, y: 0 },
113114
},
114115
{
115116
breakpoint: breakpoints.desktop,
116117
width: 940,
117118
aspectRatio: '5:3',
119+
cropOffset: { x: 50, y: 0 },
118120
},
119121
];
120122
}

dotcom-rendering/src/components/Picture.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export type ImageWidthType = {
4040
breakpoint: number;
4141
width: number;
4242
aspectRatio?: AspectRatio;
43+
cropOffset?: { x: number; y: number };
4344
};
4445

4546
/**
@@ -403,7 +404,7 @@ export const generateSources = (
403404
imageWidths
404405
.slice()
405406
.sort(descendingByBreakpoint)
406-
.map(({ width: imageWidth, breakpoint, aspectRatio }) => {
407+
.map(({ width: imageWidth, breakpoint, aspectRatio, cropOffset }) => {
407408
return {
408409
breakpoint,
409410
width: imageWidth,
@@ -412,12 +413,14 @@ export const generateSources = (
412413
imageWidth,
413414
resolution: 'high',
414415
aspectRatio,
416+
cropOffset,
415417
}),
416418
lowResUrl: generateImageURL({
417419
mainImage,
418420
imageWidth,
419421
resolution: 'low',
420422
aspectRatio,
423+
cropOffset,
421424
}),
422425
};
423426
});

dotcom-rendering/src/lib/image.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,27 @@ export const generateImageURL = ({
4949
imageWidth,
5050
resolution,
5151
aspectRatio = 'none',
52+
cropOffset,
5253
}: {
5354
mainImage: string;
5455
imageWidth: number;
5556
resolution: 'low' | 'high';
5657
aspectRatio?: string;
58+
cropOffset?: { x: number; y: number };
5759
}): string => {
5860
const url = new URL(mainImage);
59-
61+
const offset = cropOffset
62+
? `,offset-x${cropOffset.x},offset-y${cropOffset.y}`
63+
: ``;
64+
const crop = `${aspectRatio}${offset}`;
6065
// In CODE, we do not generate optimised replacement images
6166
if (url.hostname === 's3-eu-west-1.amazonaws.com') return url.href;
6267

6368
const params = new URLSearchParams({
6469
width: imageWidth.toString(),
6570
dpr: String(resolution === 'high' ? 2 : 1),
6671
s: 'none',
67-
crop: aspectRatio,
72+
crop,
6873
});
6974

7075
return `https://i.guim.co.uk/img/${getServiceFromUrl(url)}${

0 commit comments

Comments
 (0)