Skip to content

Commit 67aa6c3

Browse files
authored
Merge pull request #2 from duncanmcclean/patch-1
Accept asset IDs, Paths & URLs as parameters
2 parents 01a6a0b + da4434d commit 67aa6c3

2 files changed

Lines changed: 82 additions & 27 deletions

File tree

Readme.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,23 @@ This add-on provides a number of tags:
2929

3030
### Outputting a BlurHash image
3131

32-
`{{ blur_hash:image }}` or `{{ blur_hash image="path_or_asset" }}`
32+
`{{ blur_hash:image }}`
33+
34+
or
35+
36+
`{{ blur_hash :image="asset" }}`
37+
38+
or
39+
40+
`{{ blur_hash :url="url" }}`
41+
42+
or
43+
44+
`{{ blur_hash :id="asset_id" }}`
45+
46+
or
47+
48+
`{{ blur_hash :path="local_path" }}`
3349

3450
This tag will output an encoded image in the following format:
3551

@@ -56,6 +72,18 @@ it will then be found at `resources/views/vendor/statamic-blurhash/output.blade.
5672

5773
`{{ blur_hash:encode image="path_or_asset" }}`
5874

75+
or
76+
77+
`{{ blur_hash:encode :url="url" }}`
78+
79+
or
80+
81+
`{{ blur_hash:encode :id="asset_id" }}`
82+
83+
or
84+
85+
`{{ blur_hash:encode :path="path" }}`
86+
5987
This will return a BlurHash encoded URL, useful if you want to return this to JavaScript or a 3rd party service (such as Algolia).
6088

6189

src/Tags/BlurHash.php

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use Bepsvpt\Blurhash\Facades\BlurHash as BlurHashFacade;
66
use Statamic\Contracts\Assets\Asset as AssetContract;
7+
use Statamic\Facades\Asset;
78
use Statamic\Tags\Concerns\RendersAttributes;
89
use Statamic\Tags\Tags;
910

1011
class 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

Comments
 (0)