ImageUploader.php 1.51 KB
<?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());
        }
    }
}