Code Monkey home page Code Monkey logo

php-blurhash's Introduction

php-blurhash Tests Coverage Status Latest Stable Version

A pure PHP implementation of Blurhash. The API is stable, however the hashing function in either direction may not be.

Blurhash is an algorithm written by Dag Ågren for Wolt (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.

Installation

$ composer require kornrunner/blurhash

Usage

Encoding with GD

Encoding an image to blurhash expects two-dimensional array of colors of image pixels, sample code:

<?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

Encoding with 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

Decoding with JS / TS

For decoding of 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

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');

Contributing

Issues, feature requests or improvements welcome!

Licence

This project is licensed under the MIT License.

Stargazing

Star History Chart

php-blurhash's People

Contributors

benmorel avatar jeromegamez avatar kornrunner avatar mattsches avatar quentin-st avatar staabm avatar stayallive avatar stevemoretz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-blurhash's Issues

What would be the workflow for working with full hd images.

My php code crashes when i want to upload images that are bigger then example images. The memory got filled up to the max when analyzing all the pixels in the images.

$row[] = [$colors['red'], $colors['green'], $colors['blue']];

Looks like this array got to big when using a image that is like 1920 x 1080. What would you recommend?

Decoder produces pixels with color component value = 256

This causes imagecolorallocate to fail (X component is out of range).

For example, check this blurhash: LES6SutTOH.T_Nx[$wwGD+IVT1kr

Not sure if there's a bug in decoder, or we should just use min(255, $value) in imagecolorallocate.

Too much time for encoding !!

I tried encode a small image 41 KB using the example code for PHP
it takes about 5 seconds just to encode this image to get blurhash code
Of course this is doesn't practical for performance enhancement , is there a way for faster encoding for multiple images

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.