1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArjanWestdorp\Imaginary; |
4
|
|
|
|
5
|
|
|
class Builder |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
protected $key; |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $manipulations; |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $url; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Builder constructor. |
22
|
|
|
* |
23
|
|
|
* @param string $key |
24
|
|
|
*/ |
25
|
15 |
|
public function __construct($key) |
26
|
|
|
{ |
27
|
15 |
|
$this->key = $key; |
28
|
15 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Make a circle of the image with the given radius. |
32
|
|
|
* |
33
|
|
|
* @param string $radius |
34
|
|
|
* @return $this |
35
|
|
|
*/ |
36
|
2 |
|
public function circle($radius = 'max') |
37
|
|
|
{ |
38
|
2 |
|
$this->manipulations['r'] = $radius; |
39
|
|
|
|
40
|
2 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Fit the image when resizing it. |
45
|
|
|
* |
46
|
|
|
* @param null|string $gravity |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
5 |
|
public function fit($gravity = null) |
50
|
|
|
{ |
51
|
5 |
|
$this->manipulations['c'] = 'fit'; |
52
|
|
|
|
53
|
5 |
|
if (!is_null($gravity)) { |
54
|
4 |
|
$this->manipulations['g'] = $gravity; |
55
|
4 |
|
} |
56
|
|
|
|
57
|
5 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set the desired height of the image. |
62
|
|
|
* |
63
|
|
|
* @param int $height |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
3 |
|
public function height($height) |
67
|
|
|
{ |
68
|
3 |
|
$this->manipulations['h'] = $height; |
69
|
|
|
|
70
|
3 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the desired width of the image. |
75
|
|
|
* |
76
|
|
|
* @param int $width |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
6 |
|
public function width($width) |
80
|
|
|
{ |
81
|
6 |
|
$this->manipulations['w'] = $width; |
82
|
|
|
|
83
|
6 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the desired dpr of the image. |
88
|
|
|
* |
89
|
|
|
* @param mixed $dpr |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function dpr($dpr = 'auto') |
93
|
|
|
{ |
94
|
11 |
|
$this->manipulations['dpr'] = $dpr; |
95
|
10 |
|
|
96
|
11 |
|
return $this; |
97
|
|
|
} |
98
|
11 |
|
|
99
|
|
|
/** |
100
|
|
|
* Get the string representation of all manipulations that |
101
|
|
|
* need to be executed on the given resource. |
102
|
|
|
* |
103
|
|
|
* @return string|null |
104
|
|
|
*/ |
105
|
|
|
public function getManipulations() |
106
|
11 |
|
{ |
107
|
|
|
$manipulations = array_map(function ($key) { |
108
|
11 |
|
return $key . '_' . $this->manipulations[$key]; |
109
|
|
|
}, array_keys($this->manipulations ?: [])); |
110
|
|
|
|
111
|
|
|
return implode(',', $manipulations); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the key where we build the url for. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getKey() |
120
|
|
|
{ |
121
|
|
|
return $this->key; |
122
|
|
|
} |
123
|
|
|
} |