Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

672 B
Raw Blame
import type { MediaLogger } from './logger.ts'
import sharp from 'sharp'
import { rgbaToThumbHash } from 'thumbhash'

/** Generate a ThumbHash from a downscaled image; return null when decoding fails. */
export const generateBlurHash = async (
  buffer: Buffer,
  logger?: MediaLogger,
) => {
  try {
    const { data, info } = await sharp(buffer)
      .resize(100, 100, { fit: 'inside' })
      .raw()
      .ensureAlpha()
      .toBuffer({
        resolveWithObject: true,
      })

    const thumbHash = rgbaToThumbHash(info.width, info.height, data)
    return thumbHash
  } catch (error) {
    logger?.error('Generate thumbhash failed', error)
    return null
  }
}