kornrunner / blurhash
Blurhash 的纯 PHP 实现
v1.2.2
2022-07-13 19:38 UTC
Requires
- php: ^7.3|^8.0
Requires (Dev)
- ext-gd: *
- ocramius/package-versions: ^1.4|^2.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9
- vimeo/psalm: ^4.3
README
这是 Blurhash 的纯 PHP 实现。[Blurhash](https://github.com/woltapp/blurhash)。API 是稳定的,但任意方向的哈希函数可能不稳定。
Blurhash 是由 [Dag Ågren](https://github.com/DagAgren) 为 [Wolt (woltapp/blurhash)](https://github.com/woltapp/blurhash) 编写的算法,可以将图像编码为短 (~20-30 字节) 的 ASCII 字符串。当你将字符串解码回图像时,你得到代表原始图像的颜色渐变。这在需要图像占位符的情况或甚至用于屏蔽图像内容(例如 [Mastodon](https://blog.joinmastodon.org/2019/05/improving-support-for-adult-content-on-mastodon/))时非常有用。
安装
$ composer require kornrunner/blurhash
用法
使用 GD 进行编码
编码图像为 blurhash 需要图像像素颜色的二维数组,示例代码
<?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
使用 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
使用 JS/TS 进行解码
对于 blurhash 的解码,人们可能会选择其他实现([JavaScript/TypeScript](https://github.com/woltapp/blurhash/tree/master/TypeScript))。PHP 解码器返回一个像素数组,可以用来生成图像。
<?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');
贡献
欢迎问题、功能请求或改进!
许可协议
本项目采用 [MIT 许可协议](https://github.com/kornrunner/php-blurhash/blob/HEAD/LICENSE)。