Imgix   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 138
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 19 3
A fit() 0 9 1
A crop() 0 9 1
A resize() 0 8 1
A options() 0 5 1
A extractMetaDetails() 0 20 3
A buildUrl() 0 7 1
1
<?php
2
3
namespace Riclep\Storyblok\Support\ImageTransformers;
4
5
use Illuminate\Support\Str;
6
use Imgix\UrlBuilder;
7
8
class Imgix extends BaseTransformer
9
{
10
11
	/**
12
	 * Resize the image to the given dimensions
13
	 *
14
	 * @param int $width
15
	 * @param int $height
16
	 * @return $this
17
	 */
18
	public function resize(int $width = 0, int $height = 0): self
19
	{
20
		$this->transformations = array_merge($this->transformations, [
21
			'w' => $width,
22
			'h' => $height,
23
		]);
24
25
		return $this;
26
	}
27
28
	/**
29
	 * Fit the image in the given dimensions
30
	 *
31
	 * @param string $mode
32
	 * @param array $options
33
	 * @return $this
34
	 */
35
	public function fit(string $mode, array $options = []): self
36
	{
37
		$this->transformations = array_merge($this->transformations, [
38
			'fit' => $mode
39
		]);
40
41
		$this->transformations = array_merge($this->transformations, $options);
42
43
		return $this;
44
	}
45
46
	/**
47
	 * Specify the crop type to use for the image
48
	 *
49
	 * @param string $mode
50
	 * @param array $options
51
	 * @return $this
52
	 */
53
	public function crop(string $mode, array $options = []): self
54
	{
55
		$this->transformations = array_merge($this->transformations, [
56
			'crop' => $mode
57
		]);
58
59
		$this->transformations = array_merge($this->transformations, $options);
60
61
		return $this;
62
	}
63
64
	/**
65
	 * Set the image format you want returned
66
	 *
67
	 * @param string $format
68
	 * @param int|null $quality
69
	 * @return $this
70
	 */
71
	public function format(string $format, int $quality = null): self
72
	{
73
		if ($format === 'auto') {
74
			$this->transformations = array_merge($this->transformations, [
75
				'auto' => 'format',
76
			]);
77
		} else {
78
			$this->transformations = array_merge($this->transformations, [
79
				'fm' => $format,
80
			]);
81
82
			if ($quality !== null) {
83
				$this->transformations = array_merge($this->transformations, [
84
					'q' => $quality,
85
				]);
86
			}
87
		}
88
89
		return $this;
90
	}
91
92
	/**
93
	 * Manually set any options you want for the transformation as
94
	 * and array of key value pairs
95
	 *
96
	 * @param array $options
97
	 * @return $this
98
	 */
99
	public function options(array $options): self
100
	{
101
		$this->transformations = array_merge($this->transformations, $options);
102
103
		return $this;
104
	}
105
106
107
	/**
108
	 * Returns an imgix URL using their builder
109
	 *
110
	 * @return string
111
	 */
112
	public function buildUrl(): string
113
	{
114
		$builder = new UrlBuilder(config('storyblok.imgix_domain'));
115
		$builder->setUseHttps(true);
116
		$builder->setSignKey(config('storyblok.imgix_token'));
117
118
		return $builder->createURL($this->image->content()['filename'], $this->transformations);
119
	}
120
121
	/**
122
	 * Gets the image meta from the given Storyblok URL
123
	 *
124
	 * @return void
125
	 */
126
	protected function extractMetaDetails(): void
127
	{
128
		$path = $this->image->content()['filename'];
129
130
		preg_match_all('/(?<width>\d+)x(?<height>\d+).+\.(?<extension>[a-z]{3,4})/mi', $path, $dimensions, PREG_SET_ORDER, 0);
131
132
		if ($dimensions) {
133
			if (Str::endsWith(strtolower($this->image->content()['filename']), '.svg')) {
134
				$this->meta = [
135
					'height' => false,
136
					'width' => false,
137
					'extension' => 'svg',
138
					'mime' => 'image/svg+xml',
139
				];
140
			} else {
141
				$this->meta = [
142
					'height' => $dimensions[0]['height'],
143
					'width' => $dimensions[0]['width'],
144
					'extension' => strtolower($dimensions[0]['extension']),
145
					'mime' => $this->setMime(strtolower($dimensions[0]['extension'])),
146
				];
147
			}
148
		}
149
	}
150
}