-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d74c32d
commit 7848efb
Showing
26 changed files
with
3,126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.
7848efb
There was a problem hiding this comment.
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/