An open API service indexing awesome lists of open source software.

https://github.com/kornrunner/php-blurhash

Pure PHP implementation of Blurhash (https://github.com/woltapp/blurhash)
https://github.com/kornrunner/php-blurhash

blurhash php

Last synced: about 1 month ago
JSON representation

Pure PHP implementation of Blurhash (https://github.com/woltapp/blurhash)

Awesome Lists containing this project

README

        

# php-blurhash [![Tests](https://github.com/kornrunner/php-blurhash/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/kornrunner/php-blurhash/actions/workflows/tests.yml) [![Coverage Status](https://coveralls.io/repos/github/kornrunner/php-blurhash/badge.svg?branch=master)](https://coveralls.io/github/kornrunner/php-blurhash?branch=master) [![Latest Stable Version](https://poser.pugx.org/kornrunner/blurhash/v/stable)](https://packagist.org/packages/kornrunner/blurhash)

A pure PHP implementation of [Blurhash](https://github.com/woltapp/blurhash). The API is stable, however the hashing function in either direction may not be.

Blurhash is an algorithm written by [Dag Ågren](https://github.com/DagAgren) for [Wolt (woltapp/blurhash)](https://github.com/woltapp/blurhash) that encodes an image into a short (~20-30 byte) ASCII string. When you decode the string back into an image, you get a gradient of colors that represent the original image. This can be useful for scenarios where you want an image placeholder before loading, or even to censor the contents of an image [a la Mastodon](https://blog.joinmastodon.org/2019/05/improving-support-for-adult-content-on-mastodon/).

## Installation

```sh
$ composer require kornrunner/blurhash
```

## Usage

### Encoding with GD
Encoding an image to blurhash expects two-dimensional array of colors of image pixels, sample code:

```php
read($file);
$width = $image->width();
$height = $image->height();

$pixels = [];
for ($y = 0; $y < $height; ++$y) {
$row = [];
for ($x = 0; $x < $width; ++$x) {
$colors = $image->pickColor($x, $y);

if (!($colors instanceof RgbColor)) {
$colors = $colors->convertTo(new RgbColorspace());
}

$row[] = [
$colors->channel(Red::class)->value(),
$colors->channel(Green::class)->value(),
$colors->channel(Blue::class)->value(),
];
}
$pixels[] = $row;
}

$components_x = 4;
$components_y = 3;
$blurhash = Blurhash::encode($pixels, $components_x, $components_y);
// LEHV9uae2yk8pyo0adR*.7kCMdnj
```

### Decoding with JS / TS

For decoding of blurhash people will likely go for some other implementation ([JavaScript/TypeScript](https://github.com/woltapp/blurhash/tree/master/TypeScript)).
PHP decoder returns a pixel array that can be used to generate the image:

```php