1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok\Fields; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Riclep\Storyblok\Support\ImageTransformation; |
9
|
|
|
|
10
|
|
|
class Image extends Asset |
11
|
|
|
{ |
12
|
|
|
protected $transformations = []; |
13
|
|
|
|
14
|
|
|
public function __construct($content, $block) |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
if (is_string($content)) { |
18
|
|
|
$this->upgradeOldFields($content); |
19
|
|
|
parent::__construct($this->content, $block); |
20
|
|
|
} else { |
21
|
|
|
parent::__construct($content, $block); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$this->extractMetaDetails(); |
25
|
|
|
|
26
|
|
|
if (method_exists($this, 'transformations')) { |
27
|
|
|
$this->transformations(); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function transform($name = null) { |
32
|
|
|
if ($name) { |
33
|
|
|
if (array_key_exists($name, $this->transformations) ) { |
34
|
|
|
return $this->transformations[$name]; |
35
|
|
|
} |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return new ImageTransformation($this); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function width() { |
43
|
|
|
return $this->meta('width'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function height() { |
47
|
|
|
return $this->meta('height'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function type() { |
51
|
|
|
$extension = $this->meta('extension'); |
52
|
|
|
|
53
|
|
|
if ($extension === 'jpg') { |
54
|
|
|
$extension = 'jpeg'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return 'image/' . $extension; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function picture($alt = '', $default = null, $attributes = [], $view = 'laravel-storyblok::picture-element') { |
61
|
|
|
if ($default) { |
62
|
|
|
$imgSrc = (string) $this->transformations[$default]['src']; |
63
|
|
|
} else { |
64
|
|
|
$imgSrc = $this->filename; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return view($view, [ |
68
|
|
|
'alt' => $alt, |
69
|
|
|
'attributes' => $attributes, |
70
|
|
|
'default' => $default, |
71
|
|
|
'imgSrc' => $imgSrc, |
72
|
|
|
'transformations' => $this->transformations, |
73
|
|
|
])->render(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getOriginalFilenameAttribute() { |
77
|
|
|
return $this->content['filename']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function extractMetaDetails() { |
81
|
|
|
$path = $this->content['filename']; |
82
|
|
|
|
83
|
|
|
preg_match_all('/(?<width>\d+)x(?<height>\d+).+\.(?<extension>[a-z]{3,4})/m', $path, $dimensions, PREG_SET_ORDER, 0); |
84
|
|
|
|
85
|
|
|
$this->addMeta([ |
86
|
|
|
'height' => $dimensions[0]['height'], |
87
|
|
|
'width' => $dimensions[0]['width'], |
88
|
|
|
'extension' => $dimensions[0]['extension'], |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function upgradeOldFields($content) { |
93
|
|
|
$this->content = [ |
94
|
|
|
'filename' => $content, |
95
|
|
|
'alt' => null, |
96
|
|
|
'copyright' => null, |
97
|
|
|
'fieltype' => 'asset', |
98
|
|
|
'focus' => null, |
99
|
|
|
'name' => '', |
100
|
|
|
'title' => null, |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
} |