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
|
|
|
parent::__construct($content, $block); |
17
|
|
|
|
18
|
|
|
$this->extractMetaDetails(); |
19
|
|
|
|
20
|
|
|
if (method_exists($this, 'transformations')) { |
21
|
|
|
$this->transformations(); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function transform($name = null) { |
26
|
|
|
if ($name) { |
27
|
|
|
if (array_key_exists($name, $this->transformations) ) { |
28
|
|
|
return $this->transformations[$name]; |
29
|
|
|
} |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return new ImageTransformation($this); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function width() { |
37
|
|
|
return $this->meta('width'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function height() { |
41
|
|
|
return $this->meta('height'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function type() { |
45
|
|
|
$extension = $this->meta('extension'); |
46
|
|
|
|
47
|
|
|
if ($extension === 'jpg') { |
|
|
|
|
48
|
|
|
$extension = 'jpeg'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return 'image/' . $extension; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function picture($alt = '', $default = null, $attributes = [], $view = 'laravel-storyblok::picture-element') { |
55
|
|
|
if ($default) { |
56
|
|
|
$imgSrc = (string) $this->transformations[$default]['src']; |
57
|
|
|
} else { |
58
|
|
|
$imgSrc = $this->filename; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return view($view, [ |
62
|
|
|
'alt' => $alt, |
63
|
|
|
'attributes' => $attributes, |
64
|
|
|
'default' => $default, |
65
|
|
|
'imgSrc' => $imgSrc, |
66
|
|
|
'transformations' => $this->transformations, |
67
|
|
|
])->render(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function getOriginalFilenameAttribute() { |
71
|
|
|
return $this->content['filename']; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function extractMetaDetails() { |
75
|
|
|
$path = $this->content['filename']; |
76
|
|
|
|
77
|
|
|
preg_match_all('/(?<width>\d+)x(?<height>\d+).+\.(?<extension>[a-z]{3,4})/m', $path, $dimensions, PREG_SET_ORDER, 0); |
78
|
|
|
|
79
|
|
|
$this->addMeta([ |
80
|
|
|
'height' => $dimensions[0]['height'], |
81
|
|
|
'width' => $dimensions[0]['width'], |
82
|
|
|
'extension' => $dimensions[0]['extension'], |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
} |