{"id":21270063,"url":"https://github.com/kornrunner/php-blurhash","last_synced_at":"2025-05-14T04:07:09.250Z","repository":{"id":45169299,"uuid":"194941542","full_name":"kornrunner/php-blurhash","owner":"kornrunner","description":"Pure PHP implementation of Blurhash (https://github.com/woltapp/blurhash)","archived":false,"fork":false,"pushed_at":"2025-03-13T22:07:58.000Z","size":642,"stargazers_count":681,"open_issues_count":0,"forks_count":20,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-04-14T04:58:25.752Z","etag":null,"topics":["blurhash","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kornrunner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-02T22:12:07.000Z","updated_at":"2025-04-12T11:15:54.000Z","dependencies_parsed_at":"2024-01-27T02:24:22.047Z","dependency_job_id":"dad12a34-f717-44d8-bf0c-5f175d1e662b","html_url":"https://github.com/kornrunner/php-blurhash","commit_stats":{"total_commits":127,"total_committers":8,"mean_commits":15.875,"dds":"0.10236220472440949","last_synced_commit":"b93bbe6b061b2428aa54d84e20d7ba9f47a7c528"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kornrunner%2Fphp-blurhash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kornrunner%2Fphp-blurhash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kornrunner%2Fphp-blurhash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kornrunner%2Fphp-blurhash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kornrunner","download_url":"https://codeload.github.com/kornrunner/php-blurhash/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254067718,"owners_count":22009240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blurhash","php"],"created_at":"2024-11-21T08:15:00.372Z","updated_at":"2025-05-14T04:07:04.226Z","avatar_url":"https://github.com/kornrunner.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-blurhash [![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)\n\n\nA pure PHP implementation of [Blurhash](https://github.com/woltapp/blurhash). The API is stable, however the hashing function in either direction may not be.\n\nBlurhash 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/).\n\n## Installation\n\n\n```sh\n$ composer require kornrunner/blurhash\n```\n\n## Usage\n\n### Encoding with GD\nEncoding an image to blurhash expects two-dimensional array of colors of image pixels, sample code:\n\n```php\n\u003c?php\n\nrequire_once 'vendor/autoload.php';\n\nuse kornrunner\\Blurhash\\Blurhash;\n\n$file  = 'test/data/img1.jpg';\n$image = imagecreatefromstring(file_get_contents($file));\n$width = imagesx($image);\n$height = imagesy($image);\n\n$pixels = [];\nfor ($y = 0; $y \u003c $height; ++$y) {\n    $row = [];\n    for ($x = 0; $x \u003c $width; ++$x) {\n        $index = imagecolorat($image, $x, $y);\n        $colors = imagecolorsforindex($image, $index);\n\n        $row[] = [$colors['red'], $colors['green'], $colors['blue']];\n    }\n    $pixels[] = $row;\n}\n\n$components_x = 4;\n$components_y = 3;\n$blurhash = Blurhash::encode($pixels, $components_x, $components_y);\n// LEHV9uae2yk8pyo0adR*.7kCMdnj\n```\n\n### Encoding with Intervention\n\n```php\nrequire_once 'vendor/autoload.php';\n\nuse kornrunner\\Blurhash\\Blurhash;\nuse Intervention\\Image\\Colors\\Rgb\\Channels\\Blue;\nuse Intervention\\Image\\Colors\\Rgb\\Channels\\Green;\nuse Intervention\\Image\\Colors\\Rgb\\Channels\\Red;\nuse Intervention\\Image\\Colors\\Rgb\\Color as RgbColor;\nuse Intervention\\Image\\Colors\\Rgb\\Colorspace as RgbColorspace;\nuse Intervention\\Image\\Drivers\\Gd\\Driver;\nuse Intervention\\Image\\ImageManager;\n\n$file  = 'test/data/img1.jpg';\n$manager = new ImageManager(new Driver());\n$image = $manager-\u003eread($file);\n$width = $image-\u003ewidth();\n$height = $image-\u003eheight();\n\n$pixels = [];\nfor ($y = 0; $y \u003c $height; ++$y) {\n    $row = [];\n    for ($x = 0; $x \u003c $width; ++$x) {\n        $colors = $image-\u003epickColor($x, $y);\n        \n        if (!($colors instanceof RgbColor)) {\n            $colors = $colors-\u003econvertTo(new RgbColorspace());\n        }\n\n        $row[] = [\n            $colors-\u003echannel(Red::class)-\u003evalue(),\n            $colors-\u003echannel(Green::class)-\u003evalue(),\n            $colors-\u003echannel(Blue::class)-\u003evalue(),\n        ];\n    }\n    $pixels[] = $row;\n}\n\n$components_x = 4;\n$components_y = 3;\n$blurhash = Blurhash::encode($pixels, $components_x, $components_y);\n// LEHV9uae2yk8pyo0adR*.7kCMdnj\n```\n\n### Decoding with JS / TS\n\nFor decoding of blurhash people will likely go for some other implementation ([JavaScript/TypeScript](https://github.com/woltapp/blurhash/tree/master/TypeScript)).\nPHP decoder returns a pixel array that can be used to generate the image:\n\n```php\n\u003c?php\n\nrequire_once 'vendor/autoload.php';\n\nuse kornrunner\\Blurhash\\Blurhash;\n\n$blurhash = 'LEHV6nWB2yk8pyo0adR*.7kCMdnj';\n$width    = 269;\n$height   = 173;\n\n$pixels = Blurhash::decode($blurhash, $width, $height);\n$image  = imagecreatetruecolor($width, $height);\nfor ($y = 0; $y \u003c $height; ++$y) {\n    for ($x = 0; $x \u003c $width; ++$x) {\n        [$r, $g, $b] = $pixels[$y][$x];\n        imagesetpixel($image, $x, $y, imagecolorallocate($image, $r, $g, $b));\n    }\n}\nimagepng($image, 'blurhash.png');\n```\n\n## Contributing\n\nIssues, feature requests or improvements welcome!\n\n## Licence\n\nThis project is licensed under the [MIT License](LICENSE).\n\n## Stargazing\n\n[![Star History Chart](https://api.star-history.com/svg?repos=kornrunner/php-blurhash\u0026type=Date)](https://star-history.com/#kornrunner/php-blurhash\u0026Date)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkornrunner%2Fphp-blurhash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkornrunner%2Fphp-blurhash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkornrunner%2Fphp-blurhash/lists"}