-
Notifications
You must be signed in to change notification settings - Fork 2
/
Kraken.php
215 lines (181 loc) · 5.11 KB
/
Kraken.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
* This file is part of the hanzo package.
*
* (c) Ulrik Nielsen <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Pompdelux\KrakenBundle;
use Guzzle\Service\Client;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Router;
class Kraken
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var Router
*/
protected $router;
/**
* @var Client
*/
protected $guzzle;
/**
* @var string
*/
protected $service_type;
/**
* @var string
*/
protected $api_key;
protected $api_secret;
/**
* @var null|string
*/
protected $callback_route;
/**
* @var bool
*/
protected $use_lossy;
/**
* @param Client $client
* @param LoggerInterface $logger
* @param Router $router
* @param string $api_key
* @param string $api_secret
* @param string $type
* @param bool $use_lossy
* @param string $callback_route
*/
public function __construct(Client $client, LoggerInterface $logger, Router $router, $api_key, $api_secret, $type = 'url', $use_lossy = true, $callback_route = null)
{
$this->guzzle = $client;
$this->logger = $logger;
$this->router = $router;
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->service_type = $type;
$this->use_lossy = $use_lossy;
$this->callback_route = $callback_route;
}
/**
* @param $image
* @return KrakenResponse
*/
public function squeeze($image)
{
return $this->send($image);
}
/**
* @param string $image
* @param array $sizes
* @return array
* @throws \InvalidArgumentException
*/
public function resize($image, array $sizes = [])
{
// validation first.
foreach ($sizes as $dimensions) {
if (empty($dimensions['width']) ||
empty($dimensions['height']) ||
empty($dimensions['strategy']) ||
!in_array($dimensions['strategy'], ['exact', 'portrait', 'landscape', 'auto', 'crop', 'square', 'fill'])
) {
throw new \InvalidArgumentException('Dimensions array not valid, please fix.');
}
if (('fill' === $dimensions['strategy']) && empty($dimensions['background'])) {
throw new \InvalidArgumentException('When using the "fill" strategy, you must supply a background color. HEX, rgb() or rgba() supported.');
}
}
$results = [];
foreach ($sizes as $dimensions) {
$results[] = $this->send($image, $dimensions);
}
return $results;
}
/**
* @param string $image
* @param array $dimensions
* @return KrakenResponse
*/
protected function send($image, array $dimensions = [])
{
switch ($this->service_type) {
case 'url':
$data = $this->getUrlData($image, $dimensions);
break;
case 'upload':
$data = $this->getUploadData($image, $dimensions);
break;
}
$this->logger->debug('Sending kraken request to '.$this->guzzle->getBaseUrl().' payload: '.print_r($data,1));
$request = $this->guzzle->post(null, null, ['body' => $data], ['debug' => true]);
$response = $request->send();
return new KrakenResponse($response);
}
/**
* @param string $image
* @param array $dimensions
* @param bool $as_json
* @return array|string
*/
protected function getUrlData($image, array $dimensions = [], $as_json = true)
{
$data = [
'auth' => [
'api_key' => $this->api_key,
'api_secret' => $this->api_secret
],
'wait' => $this->getWait(),
'callback_url' => $this->getCallbackUrl(),
'url' => $image,
'lossy' => $this->use_lossy,
];
if (count($dimensions)) {
$data['resize'] = $dimensions;
}
return $as_json
? json_encode($data)
: $data
;
}
/**
* @param string $image
* @param array $dimensions
* @return array
*/
protected function getUploadData($image, array $dimensions = [])
{
$data = $this->getUrlData($image, $dimensions, false);
unset($data['url']);
return [
'file' => '@'.$image,
'data' => json_encode($data)
];
}
/**
* @return string
*/
private function getCallbackUrl()
{
return $this->callback_route
? $this->router->generate($this->callback_route)
: ''
;
}
/**
* @return bool
*/
private function getWait()
{
return $this->callback_route
? false
: true
;
}
}