-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd0182d
commit 18912b9
Showing
7 changed files
with
81 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import fs from 'node:fs/promises'; | ||
import { getImages } from './src/getImages.mjs'; | ||
import fs from 'node:fs/promises' | ||
import { ImageMetaFetcher } from './src/ImageMetaFetcher.mjs' | ||
|
||
// Get all images inside the folder images | ||
const images = await getImages('images/*.{jpg,png}'); | ||
const images = await ImageMetaFetcher('images/*.{jpg,jpeg,png}') | ||
|
||
fs.writeFile('images.json', JSON.stringify(images, null, 2)); | ||
fs.writeFile('images.json', JSON.stringify(images, null, 2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { promises as fs } from 'fs' | ||
import { glob } from 'glob' | ||
import { getPlaiceholder } from 'plaiceholder' | ||
|
||
export async function ImageMetaFetcher(pattern) { | ||
try { | ||
const files = glob.sync(pattern, { posix: true }) | ||
const imagePromises = files.map(async (file) => { | ||
const src = file.replace(/^.*[\\\/]/, '') | ||
const buffer = await fs.readFile(file) | ||
const { | ||
metadata: { height, width }, | ||
base64, | ||
} = await getPlaiceholder(buffer) | ||
return { src, width, height, base64 } | ||
}) | ||
|
||
const images = await Promise.all(imagePromises) | ||
|
||
images.sort((a, b) => a.src.localeCompare(b.src)) | ||
|
||
return images | ||
} catch (error) { | ||
console.error('Error fetching images:', error) | ||
throw error | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { test } from 'uvu' | ||
import * as assert from 'uvu/assert' | ||
import { ImageMetaFetcher } from '../src/ImageMetaFetcher.mjs' | ||
|
||
const images = await ImageMetaFetcher('images/*.{jpg,jpeg,png}') | ||
|
||
test('should return an array of images', () => { | ||
assert.type(images, 'object') | ||
assert.equal(images.length, 2) | ||
|
||
assert.type(images[0].src, 'string') | ||
assert.type(images[0].width, 'number') | ||
assert.type(images[0].height, 'number') | ||
assert.type(images[0].base64, 'string') | ||
}) | ||
|
||
test('should sort images alphabetically', () => { | ||
assert.equal(images[0].src, 'eveling-salazar-TqikiXaDrf4-unsplash.jpg') | ||
assert.equal(images[1].src, 'mo-ZLBmpXhbzMk-unsplash.jpg') | ||
}) | ||
|
||
test('should get width and height of images', () => { | ||
assert.equal(images[0].width, 480) | ||
assert.equal(images[0].height, 600) | ||
|
||
assert.equal(images[1].width, 480) | ||
assert.equal(images[1].height, 613) | ||
}) | ||
|
||
test('should get base64 of images', () => { | ||
assert.equal( | ||
images[0].base64, | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAECAIAAADETxJQAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAMUlEQVR4nGP48+TZnJL82cW5DAuampNM9fdP7WNQYWCwEBK6vnM7Q0FAQK6P7/+vXwFwfBHUaTcNcAAAAABJRU5ErkJggg==', | ||
) | ||
assert.equal( | ||
images[1].base64, | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAAECAIAAADETxJQAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAM0lEQVR4nAEoANf/AKjg/6zf/7jr/wDt//+0vsPq5+QALjFWNDZOa15vAAAANAAFOgACOPlHEtuYfOsCAAAAAElFTkSuQmCC', | ||
) | ||
}) | ||
|
||
test.run() |
This file was deleted.
Oops, something went wrong.