1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2024-12-23 09:42:04 +01:00
portfolio/scripts/content/images.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

import fs from 'node:fs'
import { join } from 'node:path'
import type { ImageType } from '@/types/image'
import sharp from 'sharp'
2024-02-06 00:20:48 +01:00
import { rgbDataURL } from './rgbDataURL'
const imagesDirectory = join(process.cwd(), 'public', 'images')
export async function getProjectImages(slug: string) {
const allImages = fs.readdirSync(imagesDirectory, 'utf8')
const projectImages = allImages.filter((image) => image.includes(slug))
let images: ImageType[] = []
await Promise.all(
projectImages.map(async (image) => {
const file = `${imagesDirectory}/${image}`
const transformer = sharp(file)
const { width, height, format } = await transformer.metadata()
const { dominant } = await transformer.stats()
const blurDataURL = rgbDataURL(dominant.r, dominant.g, dominant.b)
const imageType: ImageType = {
width,
height,
format,
blurDataURL,
src: `/images/${image}`
}
images.push(imageType)
})
)
// Sort images by sequentially numbered name to be sure
images = images.sort((a, b) => a.src.localeCompare(b.src))
return images
}