PHP Blurhash

Blurhash 的纯 PHP 实现(https://github.com/woltapp/blurhash)。「Pure PHP implementation of Blurhash (https://github.com/woltapp/blurhash)

Github星跟踪图

php-blurhash Tests Coverage Status Latest Stable Version

A pure PHP implementation of Blurhash. The API is stable, however the hashing function in either direction may not be.

Blurhash is an algorithm written by Dag Ågren for Wolt (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.

Installation

$ 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

require_once 'vendor/autoload.php';

use kornrunner\Blurhash\Blurhash;

$file  = 'test/data/img1.jpg';
$image = imagecreatefromstring(file_get_contents($file));
$width = imagesx($image);
$height = imagesy($image);

$pixels = [];
for ($y = 0; $y < $height; ++$y) {
    $row = [];
    for ($x = 0; $x < $width; ++$x) {
        $index = imagecolorat($image, $x, $y);
        $colors = imagecolorsforindex($image, $index);

        $row[] = [$colors['red'], $colors['green'], $colors['blue']];
    }
    $pixels[] = $row;
}

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

Encoding with Intervention

require_once 'vendor/autoload.php';

use kornrunner\Blurhash\Blurhash;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;

$file  = 'test/data/img1.jpg';
$manager = new ImageManager(new Driver());
$image = $manager->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).
PHP decoder returns a pixel array that can be used to generate the image:

<?php

require_once 'vendor/autoload.php';

use kornrunner\Blurhash\Blurhash;

$blurhash = 'LEHV6nWB2yk8pyo0adR*.7kCMdnj';
$width    = 269;
$height   = 173;

$pixels = Blurhash::decode($blurhash, $width, $height);
$image  = imagecreatetruecolor($width, $height);
for ($y = 0; $y < $height; ++$y) {
    for ($x = 0; $x < $width; ++$x) {
        [$r, $g, $b] = $pixels[$y][$x];
        imagesetpixel($image, $x, $y, imagecolorallocate($image, $r, $g, $b));
    }
}
imagepng($image, 'blurhash.png');

Contributing

Issues, feature requests or improvements welcome!

Licence

This project is licensed under the MIT License.

Stargazing

Star History Chart

主要指标

概览
名称与所有者kornrunner/php-blurhash
主编程语言PHP
编程语言PHP (语言数: 1)
平台Linux, Mac, Windows
许可证MIT License
所有者活动
创建于2019-07-02 22:12:07
推送于2025-03-13 22:07:58
最后一次提交
发布数5
最新版本名称v1.2.2 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数698
关注者数9
派生数20
提交数138
已启用问题?
问题数3
打开的问题数0
拉请求数8
打开的拉请求数0
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?