Skip to content

Commit cb32ff0

Browse files
committed
Fix image service ratio.
1 parent bb932d6 commit cb32ff0

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

src/image.service.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,9 @@ const service: LocalImageService = {
220220
inferSize?: boolean;
221221
}
222222
): Promise<{ data: Uint8Array; format: ImageOutputFormat }> {
223+
const sharp = (await import("sharp")).default;
223224
const MAX_WIDTH = 1280;
224-
const MAX_HEIGHT = 720;
225+
const MAX_HEIGHT = 1280;
225226
let buffer = inputBuffer;
226227

227228
if (/^https?:\/\//.test(transform.src)) {
@@ -236,30 +237,51 @@ const service: LocalImageService = {
236237
}
237238
}
238239

239-
let width = transform.width
240-
? Math.min(transform.width, MAX_WIDTH)
241-
: undefined;
242-
let height = transform.height
243-
? Math.min(transform.height, MAX_HEIGHT)
244-
: undefined;
240+
let width = transform.width;
241+
let height = transform.height;
245242

246-
if (!transform.width || !transform.height) {
247-
try {
248-
const sharp = (await import("sharp")).default;
249-
const metadata = await sharp(buffer).metadata();
250-
if (!width && metadata.width) {
251-
width = metadata.width;
252-
}
253-
if (!height && metadata.height) {
254-
height = metadata.height;
243+
try {
244+
const metadata = await sharp(buffer).metadata();
245+
246+
width = metadata.width;
247+
height = metadata.height;
248+
249+
// Ensure width and height are both defined before calculating aspect ratio
250+
if (metadata.width && metadata.height) {
251+
const aspectRatio = metadata.width / metadata.height;
252+
253+
// Resize the image while maintaining the aspect ratio and ensuring it fits within the 1280x1280 square
254+
if (width) {
255+
// If only width is defined, calculate the height to maintain the aspect ratio
256+
height = Math.round(width / aspectRatio);
257+
if (height > MAX_HEIGHT) {
258+
height = MAX_HEIGHT;
259+
width = Math.round(height * aspectRatio);
260+
}
261+
} else if (height) {
262+
// If only height is defined, calculate the width to maintain the aspect ratio
263+
width = Math.round(height * aspectRatio);
264+
if (width > MAX_WIDTH) {
265+
width = MAX_WIDTH;
266+
height = Math.round(width / aspectRatio);
267+
}
268+
} else {
269+
// If neither width nor height is defined, ensure the image fits within the max width and height
270+
if (metadata.width > metadata.height) {
271+
width = Math.min(metadata.width, MAX_WIDTH);
272+
height = Math.round(width / aspectRatio);
273+
} else {
274+
height = Math.min(metadata.height, MAX_HEIGHT);
275+
width = Math.round(height * aspectRatio);
276+
}
255277
}
256-
} catch (err) {
257-
console.warn(`Error inferring image size: ${err}`);
258278
}
279+
} catch (err) {
280+
console.warn(`Error inferring image size: ${err}`);
259281
}
260282

283+
// Proceed with resizing and transformation
261284
try {
262-
const sharp = (await import("sharp")).default;
263285
let image = sharp(buffer, { failOn: "none" });
264286
image = image.resize(width, height);
265287

@@ -277,6 +299,7 @@ const service: LocalImageService = {
277299
} catch (err) {
278300
console.warn(`⚠️ Error processing image: ${transform.src}`, err);
279301

302+
// Handle fallback
280303
try {
281304
const placeholderBuffer = await createPlaceholderImage(
282305
transform.width || 400,

src/pages/speaker/[slug].astro

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ export async function getStaticPaths() {
1616
}
1717
1818
const {entry} = Astro.props;
19-
console.log(entry.data.avatar);
2019
2120
let avatar: any;
2221
2322
if (entry.data.avatar){
2423
try {
25-
avatar = getImage({ src: entry.data.avatar, alt: 'User avatar' });
24+
avatar = await getImage({ src: entry.data.avatar, inferSize: true, format: "webp", alt: 'User avatar' });
2625
} catch (e) {
2726
//TODO: improve placeholders and offline
2827
//avatar = await getImage({ src: 'https://placehold.co/600x400?text=x', width: '600', height:'400', alt: 'Default avatar' });
@@ -91,7 +90,7 @@ function ensureHttps(str: string): string {
9190
(entry.data.avatar &&
9291
<div class="border-4 border-white rounded-lg shadow-lg inline-block mb-10">
9392
<Picture
94-
src={avatar}
93+
src={avatar.src}
9594
alt={entry.data.name}
9695
inferSize={true}
9796
widths={[400, 800, 1200]}

0 commit comments

Comments
 (0)