Passed
Push — master ( 3242af...43b283 )
by Richard
17:32 queued 10s
created

Image::height()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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') {
0 ignored issues
show
introduced by
The condition $extension === 'jpg' is always false.
Loading history...
48
			$extension = 'jpeg';
49
		}
50
51
		return 'image/' . $extension;
0 ignored issues
show
Bug introduced by
Are you sure $extension of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
		return 'image/' . /** @scrutinizer ignore-type */ $extension;
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property filename does not exist on Riclep\Storyblok\Fields\Image. Since you implemented __get, consider adding a @property annotation.
Loading history...
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
}