1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Luilliarcec\LaravelImgur\Support; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Illuminate\Http\UploadedFile; |
7
|
|
|
use Luilliarcec\LaravelImgur\Exceptions\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class LaravelImgur extends ImgurResponse |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Api connection url |
13
|
|
|
*/ |
14
|
|
|
private $apiUrl; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base url access |
18
|
|
|
*/ |
19
|
|
|
private $baseUrl; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Guzzle client for request |
23
|
|
|
* |
24
|
|
|
* @var Client |
25
|
|
|
*/ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Image to upload |
30
|
|
|
*/ |
31
|
|
|
private $image; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Request Header |
35
|
|
|
*/ |
36
|
|
|
private $headers = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Parameters Request Body |
40
|
|
|
*/ |
41
|
|
|
private $body = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Thumbnails available |
45
|
|
|
*/ |
46
|
|
|
protected $thumbnails = ['s', 'b', 't', 'm', 'l', 'h']; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Imgur constructor. |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
|
|
public function __construct() |
53
|
|
|
{ |
54
|
|
|
$this->client = new Client(); |
55
|
|
|
$this->apiUrl = config('imgur.api_url'); |
56
|
|
|
$this->baseUrl = config('imgur.base_url'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set header request |
61
|
|
|
* |
62
|
|
|
* @param array $headers |
63
|
|
|
*/ |
64
|
|
|
public function setHeaders(array $headers) |
65
|
|
|
{ |
66
|
|
|
$this->headers = $headers; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get header request |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
private function getHeaders(): array |
75
|
|
|
{ |
76
|
|
|
if (empty($this->headers)) { |
77
|
|
|
return [ |
78
|
|
|
'headers' => [ |
79
|
|
|
'authorization' => 'Client-ID ' . config('imgur.client_id'), |
80
|
|
|
'content-type' => 'application/x-www-form-urlencoded', |
81
|
|
|
] |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->headers; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set body request |
90
|
|
|
* |
91
|
|
|
* @param array $params |
92
|
|
|
*/ |
93
|
|
|
public function setBody(array $params) |
94
|
|
|
{ |
95
|
|
|
$this->body = $params; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get body request |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
private function getBody(): array |
104
|
|
|
{ |
105
|
|
|
if (empty($this->body)) { |
106
|
|
|
return [ |
107
|
|
|
'form_params' => [ |
108
|
|
|
'image' => $this->image |
109
|
|
|
] |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->body; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set response |
118
|
|
|
* |
119
|
|
|
* @param mixed $response |
120
|
|
|
*/ |
121
|
|
|
public function setResponse($response) |
122
|
|
|
{ |
123
|
|
|
$this->response = $response; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get response |
128
|
|
|
* |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
public function getResponse() |
132
|
|
|
{ |
133
|
|
|
return $this->response; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Upload the resource to imgur |
138
|
|
|
* |
139
|
|
|
* @param string|UploadedFile $image |
140
|
|
|
* @return mixed |
141
|
|
|
* @throws InvalidArgumentException |
142
|
|
|
*/ |
143
|
|
|
public function upload($image) |
144
|
|
|
{ |
145
|
|
|
$this->image = $this->fileType($image); |
146
|
|
|
|
147
|
|
|
$response = $this->client->post($this->apiUrl, array_merge($this->getHeaders(), $this->getBody())); |
148
|
|
|
|
149
|
|
|
$this->setResponse(json_decode($response->getBody()->getContents())); |
150
|
|
|
|
151
|
|
|
$this->fillData(); |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Remove an image by its deleteHash |
158
|
|
|
* |
159
|
|
|
* @param string $hash |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function remove(string $hash) |
163
|
|
|
{ |
164
|
|
|
$response = $this->client->delete($this->apiUrl . '/' . $hash, $this->getHeaders()); |
165
|
|
|
|
166
|
|
|
$this->setResponse(json_decode($response->getBody()->getContents())); |
167
|
|
|
|
168
|
|
|
$this->flushData(); |
169
|
|
|
|
170
|
|
|
return $this->getResponse()->success; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Convert the image to thumbnails depending on the size. |
175
|
|
|
* |
176
|
|
|
* @param string $link |
177
|
|
|
* @param string $size |
178
|
|
|
* @return string |
179
|
|
|
* @throws InvalidArgumentException |
180
|
|
|
*/ |
181
|
|
|
public function thumbnails(string $link, string $size) |
182
|
|
|
{ |
183
|
|
|
if (!in_array($size, $this->thumbnails)) { |
184
|
|
|
throw new InvalidArgumentException("Imgur does not support this {$size} type."); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this->getImageThumbnails($link, $size); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Check that the file type is an UploadedFile instance or a url |
192
|
|
|
* |
193
|
|
|
* @param string|UploadedFile $image |
194
|
|
|
* @return string |
195
|
|
|
* @throws InvalidArgumentException |
196
|
|
|
*/ |
197
|
|
|
private function fileType($image) |
198
|
|
|
{ |
199
|
|
|
if ($image instanceof UploadedFile) { |
200
|
|
|
return base64_encode(file_get_contents($image->path())); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (filter_var($image, FILTER_VALIDATE_URL)) { |
204
|
|
|
return $image; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
throw new InvalidArgumentException("Invalid argument provided."); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the link of the image converted to Thumbnails |
212
|
|
|
* |
213
|
|
|
* @param string $size |
214
|
|
|
* @param string $link |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
private function getImageThumbnails(string $link, string $size) |
218
|
|
|
{ |
219
|
|
|
$imageArray = explode('.', explode($this->baseUrl, $link)[1]); |
220
|
|
|
|
221
|
|
|
$image = $imageArray[0]; |
222
|
|
|
$extension = '.' . $imageArray[1]; |
223
|
|
|
|
224
|
|
|
$possibleSize = substr($image, strlen($image) - 1); |
225
|
|
|
|
226
|
|
|
if (in_array($possibleSize, $this->thumbnails)) { |
227
|
|
|
$image = substr_replace($image, $size, strlen($image) - 1); |
228
|
|
|
} else { |
229
|
|
|
$image .= $size; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $this->baseUrl . $image . $extension; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|