u
This commit is contained in:
41
src/services/companyImport/imageHasher.js
Normal file
41
src/services/companyImport/imageHasher.js
Normal file
@@ -0,0 +1,41 @@
|
||||
// MARK: - Imports
|
||||
// #region Imports
|
||||
import sharp from 'sharp';
|
||||
import { encodeRGBA } from 'skelehash-wasm';
|
||||
import { createLogger } from '../../util/logger.js';
|
||||
// #endregion
|
||||
|
||||
const log = createLogger('import');
|
||||
|
||||
// MARK: - SkeleHash Computation
|
||||
// #region SkeleHash Computation
|
||||
|
||||
/**
|
||||
* Computes SkeleHash base62 hash for an image buffer.
|
||||
*
|
||||
* @param {Buffer} buffer
|
||||
* @returns {Promise<{base62: string, width: number, height: number, hashLength: number}|null>}
|
||||
*/
|
||||
export async function computeSkeleHash(buffer) {
|
||||
try {
|
||||
const { data, info } = await sharp(buffer)
|
||||
.rotate()
|
||||
.resize({ width: 200, height: 200, fit: 'inside' })
|
||||
.ensureAlpha()
|
||||
.raw()
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
|
||||
const result = await encodeRGBA(info.width, info.height, data);
|
||||
return {
|
||||
base62: result.base62,
|
||||
width: info.width,
|
||||
height: info.height,
|
||||
hashLength: result.hash?.length ?? 0,
|
||||
};
|
||||
} catch (err) {
|
||||
log.warn('Failed to compute skelehash for image:', err.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// #endregion
|
||||
Reference in New Issue
Block a user