1010class BlurHash extends Tags
1111{
1212 use RendersAttributes;
13+
14+ private $ dimensions = false ;
1315
1416 /**
1517 * The {{ blur_hash:encode }} tag.
@@ -21,6 +23,8 @@ public function encode()
2123 $ image = $ this ->params ->get ('image ' );
2224
2325 if ($ image instanceof AssetContract) {
26+ $ this ->dimensions = $ image ->dimensions ();
27+
2428 $ image = $ image ->contents ();
2529 }
2630
@@ -55,9 +59,26 @@ public function decode($encodedString = null)
5559 public function index ()
5660 {
5761 $ image = $ this ->params ->get ('image ' );
62+
63+ $ hash = $ this ->encode ($ image );
64+
65+ if ($ this ->dimensions ) {
66+
67+ $ width = $ this ->params ->get ('width ' , 0 );
68+ $ height = $ this ->params ->get ('height ' , 0 );
69+
70+ // if we have width but not height, we work out the height proportionally
71+ if ($ width && ! $ height ) {
72+ $ this ->params ->put ('height ' , $ width * $ this ->dimensions [1 ]/$ this ->dimensions [0 ]);
73+
74+ // if we have height but not width, we work out the width proportionally
75+ } else if ($ height && ! $ width ) {
76+ $ this ->params ->put ('width ' , $ height * $ this ->dimensions [0 ]/$ this ->dimensions [1 ]);
77+ }
78+ }
5879
5980 return view ('statamic-blurhash::output ' , [
60- 'src ' => $ this ->decode ($ this -> encode ( $ image ) ),
81+ 'src ' => $ this ->decode ($ hash ),
6182 'params ' => $ this ->renderAttributesFromParams (['image ' ]),
6283 ]);
6384 }
0 commit comments