ImageUploader.php
1.51 KB
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
<?php
namespace FootyRoom\Support;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\BadResponseException;
class ImageUploader
{
/**
* Upload image to CDN.
*
* @param string $src
* @param string $bucket Bucket name on AWS where to upload to.
* @param string $name What to name the uploaded file
*
* @return string|null Returns url to uploaded image
*/
public static function upload($src, $bucket, $name)
{
try {
$client = new HttpClient(['verify' => false]);
$result = $client->post('https://admin.'.app('FootyRoom\Config')->get('domain').'/api/images', [
'multipart' => [
[
'name' => 'api_key',
'contents' => 'f402736998f5d20a40bdfc12cdeba6f0',
],
[
'name' => 'file',
'contents' => fopen($src, 'r'),
],
[
'name' => 'bucket',
'contents' => $bucket,
],
[
'name' => 'name',
'contents' => $name,
]
],
]);
$result = json_decode($result->getBody()->getContents());
return $result->url;
} catch (BadResponseException $e) {
throw new \Exception($e->getResponse()->getBody()->getContents());
}
}
}