Skip to content

Commit

Permalink
rename to ImageMetaFetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
torn4dom4n committed Aug 14, 2023
1 parent bd0182d commit 18912b9
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 81 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# getImages
# Image Meta Fetcher

This code will get all images from a folder and write them to a JSON file called `images.json`.
Get image metadata in a directory.

## Requirements

Expand All @@ -10,8 +10,8 @@ This code will get all images from a folder and write them to a JSON file called
## Getting Started

```sh
git clone https://github.com/AREA44/node-getImages
cd node-getImages
git clone https://github.com/AREA44/node-image-meta-fetcher
cd node-image-meta-fetcher
pnpm install
```

Expand Down
8 changes: 4 additions & 4 deletions index.mjs
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))
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@area44/getimages",
"version": "23.8.4",
"name": "@area44/image-meta-fetcher",
"version": "23.8.15",
"private": "true",
"description": "Get all images in a directory and write them to a JSON file",
"homepage": "https://github.com/AREA44/node-getImages#readme",
"description": "Get image metadata in a directory",
"homepage": "https://github.com/AREA44/node-image-meta-fetcher#readme",
"repository": {
"url": "git+https://github.com/AREA44/node-getImages.git"
"url": "git+https://github.com/AREA44/node-image-meta-fetcher.git"
},
"license": "MIT",
"author": "AREA44",
Expand Down
27 changes: 27 additions & 0 deletions src/ImageMetaFetcher.mjs
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
}
}
27 changes: 0 additions & 27 deletions src/getImages.mjs

This file was deleted.

41 changes: 41 additions & 0 deletions tests/ImageMetaFetcher.test.mjs
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()
41 changes: 0 additions & 41 deletions tests/getImages.test.mjs

This file was deleted.

0 comments on commit 18912b9

Please sign in to comment.