This commit is contained in:
@@ -1,19 +1,11 @@
|
||||
import { rgbaToThumbHash } from "thumbhash";
|
||||
import ExifReader from 'exifreader';
|
||||
import ExifReader from "exifreader";
|
||||
import type { ImageMetadata } from "astro";
|
||||
import sharp from "sharp";
|
||||
|
||||
let s: typeof import("sharp") | undefined;
|
||||
async function getSharp(): Promise<typeof import("sharp") | undefined> {
|
||||
if (s) return s;
|
||||
s = (await import("sharp")).default;
|
||||
return s;
|
||||
}
|
||||
|
||||
export async function generateThumbHash(image: ImageMetadata & { fsPath?: string }) {
|
||||
|
||||
const sharp = await getSharp();
|
||||
if (!sharp) return;
|
||||
|
||||
export async function generateThumbHash(
|
||||
image: ImageMetadata & { fsPath?: string },
|
||||
) {
|
||||
const scaleFactor = 100 / Math.max(image.width, image.height);
|
||||
|
||||
const smallWidth = Math.floor(image.width * scaleFactor);
|
||||
@@ -30,10 +22,9 @@ export async function generateThumbHash(image: ImageMetadata & { fsPath?: string
|
||||
const buffer = rgbaToThumbHash(smallWidth, smallHeight, smallImg);
|
||||
return Buffer.from(buffer).toString("base64");
|
||||
} catch (error) {
|
||||
console.log(`Could not generate thumbhash for ${image.fsPath}`, error)
|
||||
return ""
|
||||
console.log(`Could not generate thumbhash for ${image.fsPath}`, error);
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const allowedExif = [
|
||||
@@ -54,11 +45,8 @@ const allowedExif = [
|
||||
|
||||
export async function getExifData(image: ImageMetadata) {
|
||||
if (image.format === "svg") return undefined; // SVGs don't have EXIF data")
|
||||
const sharp = await getSharp();
|
||||
if (!sharp) return;
|
||||
const imagePath = (image as ImageMetadata & { fsPath: string }).fsPath;
|
||||
try {
|
||||
|
||||
const buffer = await sharp(imagePath).toBuffer();
|
||||
|
||||
const tags = await ExifReader.load(buffer, { async: true });
|
||||
@@ -74,9 +62,7 @@ export async function getExifData(image: ImageMetadata) {
|
||||
|
||||
return hasExif ? out : undefined;
|
||||
} catch (error) {
|
||||
|
||||
console.log(`Error reading EXIF data from ${imagePath}`, error);
|
||||
return undefined
|
||||
return undefined;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user