forked from symfony/ux
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LazyImage] Cache BlurHash, close symfony#2
- Loading branch information
Showing
6 changed files
with
145 additions
and
25 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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
use Intervention\Image\ImageManager; | ||
use kornrunner\Blurhash\Blurhash as BlurhashEncoder; | ||
use Symfony\Contracts\Cache\CacheInterface; | ||
|
||
/** | ||
* @author Titouan Galopin <[email protected]> | ||
|
@@ -21,11 +22,10 @@ | |
*/ | ||
class BlurHash implements BlurHashInterface | ||
{ | ||
private $imageManager; | ||
|
||
public function __construct(?ImageManager $imageManager = null) | ||
{ | ||
$this->imageManager = $imageManager; | ||
public function __construct( | ||
private ?ImageManager $imageManager = null, | ||
private ?CacheInterface $cache = null, | ||
) { | ||
} | ||
|
||
public function createDataUriThumbnail(string $filename, int $width, int $height, int $encodingWidth = 75, int $encodingHeight = 75): string | ||
|
@@ -62,28 +62,39 @@ public function encode(string $filename, int $encodingWidth = 75, int $encodingH | |
throw new \LogicException('To use the Blurhash feature, install kornrunner/blurhash.'); | ||
} | ||
|
||
// Resize image to increase encoding performance | ||
$image = $this->imageManager->make(file_get_contents($filename)); | ||
$image->resize($encodingWidth, $encodingHeight, static function ($constraint) { | ||
$constraint->aspectRatio(); | ||
$constraint->upsize(); | ||
}); | ||
|
||
// Encode using BlurHash | ||
$width = $image->getWidth(); | ||
$height = $image->getHeight(); | ||
|
||
$pixels = []; | ||
for ($y = 0; $y < $height; ++$y) { | ||
$row = []; | ||
for ($x = 0; $x < $width; ++$x) { | ||
$color = $image->pickColor($x, $y); | ||
$row[] = [$color[0], $color[1], $color[2]]; | ||
$doEncode = function (string $filename, int $encodingWidth, int $encodingHeight) { | ||
// Resize image to increase encoding performance | ||
$image = $this->imageManager->make(file_get_contents($filename)); | ||
$image->resize($encodingWidth, $encodingHeight, static function ($constraint) { | ||
$constraint->aspectRatio(); | ||
$constraint->upsize(); | ||
}); | ||
|
||
// Encode using BlurHash | ||
$width = $image->getWidth(); | ||
$height = $image->getHeight(); | ||
|
||
$pixels = []; | ||
for ($y = 0; $y < $height; ++$y) { | ||
$row = []; | ||
for ($x = 0; $x < $width; ++$x) { | ||
$color = $image->pickColor($x, $y); | ||
$row[] = [$color[0], $color[1], $color[2]]; | ||
} | ||
|
||
$pixels[] = $row; | ||
} | ||
|
||
$pixels[] = $row; | ||
return BlurhashEncoder::encode($pixels, 4, 3); | ||
}; | ||
|
||
if (null === $this->cache) { | ||
return $doEncode($filename, $encodingWidth, $encodingHeight); | ||
} | ||
|
||
return BlurhashEncoder::encode($pixels, 4, 3); | ||
return $this->cache->get( | ||
'blurhash.'.hash('xxh3', $filename.$encodingWidth.$encodingHeight), | ||
fn () => $doEncode($filename, $encodingWidth, $encodingHeight), | ||
); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\LazyImage\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
/** | ||
* @author Hugo Alliaume <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class Configuration implements ConfigurationInterface | ||
{ | ||
public function getConfigTreeBuilder(): TreeBuilder | ||
{ | ||
$treeBuilder = new TreeBuilder('ux_lazy_image'); | ||
$rootNode = $treeBuilder->getRootNode(); | ||
$rootNode | ||
->children() | ||
->scalarNode('cache')->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} |
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