-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
22 changed files
with
758 additions
and
1 deletion.
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,28 @@ | ||
name: Tests | ||
|
||
on: ['push', 'pull_request'] | ||
|
||
jobs: | ||
ci: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
php: [8.0] | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
tools: composer:v2 | ||
coverage: xdebug | ||
|
||
- name: Install Dependencies | ||
run: composer install --dev --no-interaction --prefer-dist --optimize-autoloader | ||
|
||
- name: Run tests | ||
run: ./vendor/bin/pest |
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,21 @@ | ||
# Ignore vendor directory | ||
/vendor | ||
|
||
# Ignore composer.lock file | ||
composer.lock | ||
|
||
# Ignore .env file (if used) | ||
.env | ||
|
||
# Ignore IDE-specific files and directories | ||
.idea/ | ||
.vscode/ | ||
*.sublime-* | ||
|
||
# Ignore log and cache directories | ||
/logs/ | ||
/cache/ | ||
|
||
# Ignore PHPUnit and Codeception configuration files | ||
phpunit.xml | ||
codeception.yml |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,38 @@ | ||
{ | ||
} | ||
"name": "srwiez/thumbhash", | ||
"description": "A brief description of your package", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Eser DENIZ", | ||
"email": "srwiez@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php": "^8.1" | ||
}, | ||
"autoload": { | ||
"files": ["src/helpers.php"], | ||
"psr-4": { | ||
"Thumbhash\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Tests\\": "tests/" | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"require-dev": { | ||
"pestphp/pest": "^2.2", | ||
"ext-imagick": "*", | ||
"ext-gd": "*" | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"pestphp/pest-plugin": true | ||
} | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
use Thumbhash\Thumbhash; | ||
use function Thumbhash\extract_size_and_pixels_with_gd; | ||
use function Thumbhash\extract_size_and_pixels_with_imagick; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$test_images = [ | ||
'../assets/sunrise.jpg', | ||
'../assets/sunset.jpg', | ||
'../assets/field.jpg', | ||
'../assets/fall.jpg', | ||
'../assets/street.jpg', | ||
'../assets/mountain.jpg', | ||
'../assets/coast.jpg', | ||
'../assets/firefox.png', | ||
'../assets/opera.png', | ||
]; | ||
|
||
echo '<h1 style="text-align: center">Thumbhash examples</h1> | ||
<table style="background-color: lightgray; text-align: center;width:80%; margin:auto;">'; | ||
|
||
foreach ($test_images as $test_image) { | ||
$url = $test_image; | ||
$path = __DIR__ . '/' . $url; | ||
$content = file_get_contents($path); | ||
|
||
list($width, $height, $pixels) = extract_size_and_pixels_with_imagick($content); | ||
$hash = Thumbhash::encode($width, $height, $pixels); | ||
|
||
$thumbBase64 = rtrim(base64_encode(implode(array_map("chr", $hash))), '='); | ||
$data_url = Thumbhash::toDataURL($hash); | ||
?> | ||
<tr> | ||
<td> | ||
<img style="width: 200px; height: auto; border: 1px solid black;" width="<?= $width; ?>" height="<?= $height; ?>" src="<?= $url; ?>" alt=""> | ||
<br><?= $width; ?> x <?= $height; ?> | ||
</td> | ||
<td> | ||
<img style="width: 200px; height: auto; border: 1px solid black;" width="<?= $width; ?>" height="<?= $height; ?>" src="<?= $data_url; ?>" alt=""> | ||
<br> | ||
<br> | ||
<?= $thumbBase64; ?> | ||
</td> | ||
</tr> | ||
|
||
|
||
<?php | ||
|
||
} | ||
echo '</table>'; |
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,47 @@ | ||
# Thumbhash PHP | ||
[![Tests](https://github.com/kornrunner/php-blurhash/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/kornrunner/php-blurhash/actions/workflows/tests.yml) [![Coverage Status](https://coveralls.io/repos/github/kornrunner/php-blurhash/badge.svg?branch=master)](https://coveralls.io/github/kornrunner/php-blurhash?branch=master) [![Latest Stable Version](https://poser.pugx.org/kornrunner/blurhash/v/stable)](https://packagist.org/packages/kornrunner/blurhash) | ||
|
||
Thumbhash PHP is a PHP library for generating unique, human-readable identifiers from image files. It is inspired by [Evan Wallace's Thumbhash algorithm](https://github.com/evanw/thumbhash) and provides a PHP implementation of the algorithm. | ||
|
||
Thumbhash is a very compact representation of a placeholder for an image. Store it inline with your data and show it while the real image is loading for a smoother loading experience. It's similar to [BlurHash](https://github.com/woltapp/blurhash) but with some advantages | ||
|
||
[Read more and test it here !](https://evanw.github.io/thumbhash/) | ||
|
||
## Installation | ||
|
||
You can install Thumbhash PHP using Composer: | ||
|
||
```bash | ||
composer require srwiez/thumbhash | ||
``` | ||
|
||
⚠️ I higly recommand to have Imagick extension installed on your computer. GD extension has only 7 bits of alpha channel resolution, and 127 is transparent, 0 opaque. While the library will still work, you may have different image between platforms. [See on stackoverflow](https://stackoverflow.com/questions/41079110/is-it-possible-to-retrieve-the-alpha-value-of-a-pixel-of-a-png-file-in-the-0-255) | ||
|
||
## Usage | ||
|
||
To generate a thumbhash for an image file, you can use the Thumbhash\Thumbhash class: | ||
|
||
Example to show a thumbhash image from a local file | ||
```php | ||
use Thumbhash\Thumbhash; | ||
|
||
$content = file_get_contents($url); | ||
|
||
list($width, $height, $pixels) = extract_size_and_pixels_with_imagick($content); | ||
|
||
$hash = Thumbhash::encode($width, $height, $pixels); | ||
$key = Thumbhash::convertHashToString($hash); // You can store this in your database as a string | ||
$url = Thumbhash::toDataURL($hash); | ||
|
||
echo '<img src="' . $url . '" />'; | ||
``` | ||
|
||
## Credits | ||
|
||
Thumbhash PHP was created by Eser DENIZ. | ||
|
||
It is inspired by the javascript version of [Evan Wallace's Thumbhash algorithm](https://github.com/evanw/thumbhash). | ||
|
||
## License | ||
|
||
Thumbhash PHP is licensed under the MIT License. See LICENSE for more information. |
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,18 @@ | ||
<?php | ||
namespace Thumbhash; | ||
final class Color { | ||
public static function toLinear(int $value): float { | ||
$value = $value / 255; | ||
return ($value <= 0.04045) | ||
? $value / 12.92 | ||
: pow(($value + 0.055) / 1.055, 2.4); | ||
} | ||
|
||
public static function tosRGB(float $value): int { | ||
$normalized = max(0, min(1, $value)); | ||
$result = ($normalized <= 0.0031308) | ||
? (int) round($normalized * 12.92 * 255 + 0.5) | ||
: (int) round((1.055 * pow($normalized, 1 / 2.4) - 0.055) * 255 + 0.5); | ||
return max(0, min($result, 255)); | ||
} | ||
}?> |
Oops, something went wrong.