44
55use Bepsvpt \Blurhash \Facades \BlurHash as BlurHashFacade ;
66use Statamic \Contracts \Assets \Asset as AssetContract ;
7+ use Statamic \Facades \Asset ;
78use Statamic \Tags \Concerns \RendersAttributes ;
89use Statamic \Tags \Tags ;
910
1011class BlurHash extends Tags
1112{
1213 use RendersAttributes;
13-
14+
1415 private $ dimensions = false ;
1516
1617 /**
@@ -20,14 +21,14 @@ class BlurHash extends Tags
2021 */
2122 public function encode ()
2223 {
23- $ image = $ this ->params -> get ( ' image ' );
24+ $ image = $ this ->getImageFromParams ( );
2425
2526 if ($ image instanceof AssetContract) {
2627 $ this ->dimensions = $ image ->dimensions ();
27-
28+
2829 $ image = $ image ->contents ();
2930 }
30-
31+
3132 return BlurHashFacade::encode ($ image );
3233 }
3334
@@ -41,15 +42,15 @@ public function decode($encodedString = null)
4142 if (! $ encodedString ) {
4243 $ encodedString = $ this ->params ->get ('hash ' );
4344 }
44-
45+
4546 $ width = $ this ->params ->get ('width ' , 64 );
4647 $ height = $ this ->params ->get ('height ' , 64 );
4748
4849 $ image = BlurHashFacade::decode ($ encodedString , $ width , $ height );
49-
50+
5051 return $ image ->encode ($ this ->params ->get ('format ' , 'data-url ' ));
5152 }
52-
53+
5354 /**
5455 * The {{ blur_hash }} tag.
5556 * Encodes and outputs an image
@@ -58,41 +59,67 @@ public function decode($encodedString = null)
5859 */
5960 public function index ()
6061 {
61- $ image = $ this ->params ->get ('image ' );
62-
63- $ hash = $ this ->encode ($ image );
64-
62+ $ hash = $ this ->encode ();
63+
6564 if ($ this ->dimensions ) {
66-
6765 $ width = $ this ->params ->get ('width ' , 0 );
6866 $ height = $ this ->params ->get ('height ' , 0 );
69-
70- // if we have width but not height, we work out the height proportionally
67+
68+ // If we have width but not height, we work out the height proportionally
7169 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 ]);
70+ $ this ->params ->put ('height ' , $ width * $ this ->dimensions [1 ] / $ this ->dimensions [0 ]);
71+
72+ // If we have height but not width, we work out the width proportionally
73+ } elseif ($ height && ! $ width ) {
74+ $ this ->params ->put ('width ' , $ height * $ this ->dimensions [0 ] / $ this ->dimensions [1 ]);
7775 }
7876 }
79-
77+
8078 return view ('statamic-blurhash::output ' , [
8179 'src ' => $ this ->decode ($ hash ),
8280 'params ' => $ this ->params ,
8381 'render_params ' => $ this ->renderAttributesFromParams (['image ' ]),
84- ]);
85- }
82+ ]);
83+ }
8684
8785 /**
8886 * The {{ blur_hash:* }} tag.
8987 *
9088 * @return string|array
91- */
89+ */
9290 public function wildcard ($ tag )
9391 {
9492 $ this ->params ->put ('image ' , $ this ->context ->value ($ tag ));
95-
93+
9694 return $ this ->index ();
97- }
98- }
95+ }
96+
97+ private function getImageFromParams ()
98+ {
99+ if ($ id = $ this ->params ->get ('id ' )) {
100+ $ image = Asset::findById ($ id );
101+
102+ if ($ image ) {
103+ return $ image ;
104+ }
105+ }
106+
107+ if ($ path = $ this ->params ->get ('path ' )) {
108+ $ image = Asset::findByPath ($ path );
109+
110+ if ($ image ) {
111+ return $ image ;
112+ }
113+ }
114+
115+ if ($ url = $ this ->params ->get ('url ' )) {
116+ $ image = Asset::findByUrl ($ url );
117+
118+ if ($ image ) {
119+ return $ image ;
120+ }
121+ }
122+
123+ return $ this ->params ->get ('image ' );
124+ }
125+ }
0 commit comments