Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
SRWieZ committed Mar 23, 2023
1 parent be4411b commit 2438cf3
Show file tree
Hide file tree
Showing 22 changed files with 758 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
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
21 changes: 21 additions & 0 deletions .gitignore
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
Binary file added assets/coast.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fall.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/field.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/firefox.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/mountain.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/opera.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/street.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sunrise.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sunset.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 37 additions & 1 deletion composer.json
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
}
}
}
52 changes: 52 additions & 0 deletions examples/test.php
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>';
47 changes: 47 additions & 0 deletions readme.md
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.
18 changes: 18 additions & 0 deletions src/Color.php
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));
}
}?>

0 comments on commit 2438cf3

Please sign in to comment.