Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
kornrunner committed Jul 5, 2019
1 parent d74c32d commit 7848efb
Show file tree
Hide file tree
Showing 26 changed files with 3,126 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service_name: travis-ci
coverage_clover: build/logs/clover.xml
json_path: coveralls-upload.json
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ composer.phar
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
build/
.phpunit.result.cache
infection.log
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: php

php:
- 7.2
- 7.3
- 7.4snapshot
- nightly

matrix:
allow_failures:
- php: nightly

before_script:
- phpenv config-add travis.php.ini

install:
- phpenv config-rm xdebug.ini || true
- COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install --prefer-dist -n

script:
- mkdir -p build/logs
- phpdbg -qrr vendor/bin/phpunit

after_script:
- travis_retry vendor/bin/php-coveralls -v
83 changes: 81 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,81 @@
# php-blurhash
Pure PHP Blurhash
# php-blurhash [![Build Status](https://travis-ci.org/kornrunner/php-blurhash.svg?branch=master)](https://travis-ci.org/kornrunner/php-blurhash) [![Coverage Status](https://coveralls.io/repos/github/kornrunner/php-blurhash/badge.svg?branch=master)](https://coveralls.io/github/kornrunner/php-blurhash?branch=master)

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 an image to blurhash expects two-dimensional array of colors of image pixels, sample code:

```php
<?php

require_once 'vendor/autoload.php';

use kornrunner\Blurhash\Blurhash;

$file = 'test/data/img1.jpg';
$image = imagecreatefromjpeg ($file);
list($width, $height) = getimagesize($file);

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

$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;

$row[] = [$r, $g, $b];
}
$pixels[] = $row;
}

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

For decoding the 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
<?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) {
$pixel = $pixels[$y][$x];
imagesetpixel($image, $x, $y, imagecolorallocate($image, $pixel[0], $pixel[1], $pixel[2]));
}
}
imagepng($image, 'blurhash.png');
```

## Contributing

Issues, feature requests or improvements welcome!

## Licence

This project is licensed under the [MIT License](LICENSE).
39 changes: 39 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "kornrunner/blurhash",
"description": "Pure PHP implementation of Blurhash",
"type": "library",
"license": "MIT",
"homepage": "https://github.com/kornrunner/php-blurhash",
"authors": [
{
"name": "Boris Momčilović",
"email": "boris.momcilovic@gmail.com"
}
],
"autoload": {
"psr-4": {
"kornrunner\\Blurhash\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"kornrunner\\Blurhash\\": "test"
}
},
"require": {
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^8.2",
"php-coveralls/php-coveralls": "^2.1"
},
"archive": {
"exclude": ["/vendor", "/test", "/build"]
},
"support": {
"source": "https://github.com/kornrunner/php-blurhash.git"
},
"config": {
"sort-packages": true
}
}
Loading

1 comment on commit 7848efb

@szepeviktor
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kornrunner Very nice code :) try running phpstan analyze --level=max src/

Please sign in to comment.