Skip to content

Blurhash implementation using TypeScript and Azure Functions

License

Notifications You must be signed in to change notification settings

desholmes/blurhash.dholmes.co.uk

Repository files navigation

BlurHash: Dholmes.co.uk

An Azure Function App to encode and decode images using the the BlurHash algorithm.

Demo and API is available at blurhash.dholmes.co.uk. Read the Blog post for more information and related posts.

Remotes for this repo:

  1. GitHub - desholmes/blurhash.dholmes.co.uk
  2. Azure DevOps - DesHolmes/blurhash.dholmes.co.uk

Usage

GET: /api/v1/blur-hash

  • Decodes a BlurHash and return a jpeg to the specified dimensions
  • Request URL GET: /api/v1/blur-hash
    • URL Params:
      • blurhash: blurhash for the the image (required)
      • width: Width of the decoded image (required)
      • height: Height of the decoded image (required)

Returns a jpg based on the URL params if the BlurHash string is valid.

Example error response (400):

{
  "error": true,
  "message": "The blurhash is invalid - blurhash length mismatch: length is 18 but it should be 28"
}

POST: /api/v1/blur-hash

  • Encodes an image and returns a BlurHash string
  • Request URL POST: /api/v1/blur-hash
    • Body Params:
      • imageUrl: URL for the image (required)

Returns BlurHash'd image if the imageUrl is valid and publicly accessible.

Example request body:

{
    "imageUrl": "https://c1.wallpaperflare.com/preview/102/822/554/beach-north-sea-sea-sunset.jpg"
}

Example error response (404):

{
  "error": true,
  "message": "imageUrl <imageUrl> could not be loaded"
}

GET: /api/v1/health-check

  • Health check route
  • Request URL GET: /api/v1/health-check

Used by monitoring to check system health.

Example response:

{
  "error": false,
  "message": "Service is available"
}

Development

Prerequisites

  1. An installation of Node.js v18.12.1 (npm v8.19.2)
  2. An installation of Azure Functions Core Tools v4
  3. An installation of VSCode with the following extensions:
    1. Spelling Checker
    2. ESLint
    3. Prettier
    4. TypeScript

Getting Started

  1. Copy ./local.settings-template.json to ./local.settings.json
  2. npm i: Installs dependencies
  3. npm run start:dev: Starts the local DEV environment
  4. Press CTRL+c: Stops the local DEV environment

Commands

Command Description
npm start Starts the Function App in Azure func start
npm start:dev Starts a local dev environment by running npm start and npm run watch concurrently
npm run build Create a build tsc
npm run watch Watch and create a build tsc tsc -w
npm run audit Runs npm audit --production --audit-level=critical to check for known vulnerabilities
npm run lint Run eslint eslint --max-warnings=0 --ext=ts,json ./
npm run lint:fix Run eslint with fix arg eslint --max-warnings=0 --fix --ext=ts,json ./

Credits

TODO

  1. Add base64 encoded property to POST: /api/v1/blur-hash response