-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using blurhash for entry thumbnail blurred background (#333)
quick and dirty thumbnail filler background image using blurhash - can be generated on twig side using `blurhash_image` component, feeding blurhash as input props - the image is embedded in `<img>` tag using `data:image/png;base64`, with default resolution of 20x20 and stretched with css for display, replacing .image-filler css background-image + blur filter
- Loading branch information
Showing
5 changed files
with
71 additions
and
14 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
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Twig\Components; | ||
|
||
use kornrunner\Blurhash\Blurhash; | ||
use Symfony\Contracts\Cache\CacheInterface; | ||
use Symfony\Contracts\Cache\ItemInterface; | ||
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; | ||
|
||
#[AsTwigComponent('blurhash_image')] | ||
final class BlurhashImageComponent | ||
{ | ||
public string $blurhash; | ||
public int $width = 20; | ||
public int $height = 20; | ||
|
||
public function __construct(private CacheInterface $cache) | ||
{ | ||
} | ||
|
||
public function createImage(string $blurhash, int $width = 20, int $height = 20): string | ||
{ | ||
return $this->cache->get( | ||
'bh_'.hash('sha256', $blurhash), | ||
function (ItemInterface $item) use ($blurhash, $width, $height) { | ||
$item->expiresAfter(3600); | ||
|
||
$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)); | ||
} | ||
} | ||
|
||
// I do not like this | ||
ob_start(); | ||
imagepng($image); | ||
$out = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
return 'data:image/png;base64,'.base64_encode($out); | ||
} | ||
); | ||
} | ||
} |
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 @@ | ||
<img src="{{ this.createImage(blurhash, width, height) }}" /> |
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