astro/packages/integrations/image/src/utils/metadata.ts
Tony Sullivan ef9345767b
WIP: [image] Fixing SSR support and improving error validation (#4013)
* fix: SSR builds were hitting an undefined error and skipping the step for copying original assets

* chore: update lockfile

* chore: adding better error validation to getImage and getPicture

* refactor: cleaning up index.ts

* refactor: moving SSG build generation logic out of the integration

* splitting build to ssg & ssr helpers, re-enabling SSR image build tests

* sharp should automatically rotate based on EXIF

* cleaning up how static images are tracked for SSG builds

* undo unrelated mod.d.ts change

* chore: add changeset
2022-07-22 23:01:56 +00:00

21 lines
545 B
TypeScript

import fs from 'fs/promises';
import sizeOf from 'image-size';
import { ImageMetadata, InputFormat } from '../types.js';
export async function metadata(src: string): Promise<ImageMetadata | undefined> {
const file = await fs.readFile(src);
const { width, height, type, orientation } = await sizeOf(file);
const isPortrait = (orientation || 0) >= 5;
if (!width || !height || !type) {
return undefined;
}
return {
src,
width: isPortrait ? height : width,
height: isPortrait ? width : height,
format: type as InputFormat,
};
}