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)
- Host: GitHub
- URL: https://github.com/kornrunner/php-blurhash
- Owner: kornrunner
- License: mit
- Created: 2019-07-02T22:12:07.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2025-03-13T22:07:58.000Z (4 months ago)
- Last Synced: 2025-04-14T04:58:25.752Z (2 months ago)
- Topics: blurhash, php
- Language: PHP
- Homepage:
- Size: 627 KB
- Stars: 681
- Watchers: 10
- Forks: 20
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# php-blurhash [](https://github.com/kornrunner/php-blurhash/actions/workflows/tests.yml) [](https://coveralls.io/github/kornrunner/php-blurhash?branch=master) [](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