/
var
/
www
/
html
/
restaurants
/
src
/
Service
/
Upload File
HOME
<?php namespace App\Service; use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; use Tinify; class FileUploader { private $credentials; private $s3Bucket; public function __construct($s3credentials, $s3Bucket, $tinifyKey) { $this->credentials = $s3credentials; $this->s3Bucket = $s3Bucket; Tinify\setKey($tinifyKey); } public function upload($file, $place) { if (!array_key_exists('tmp_name', $file) || !$file['tmp_name']) { return false; } $parts = explode(".", $file['name']); $ext = $parts[count($parts)-1]; $fileName = $place.'-'.uniqid().'.'.$ext; $imagePath = "/places/images/"; $source = Tinify\fromFile($file['tmp_name']); $resized = $source->resize([ "method" => "cover", "width" => 500, "height" => 500 ]); $this->credentials['path'] = $this->s3Bucket . $imagePath . $fileName; $resized->store($this->credentials); return sprintf("https://cravvingsapp.s3.eu-west-2.amazonaws.com%s%s", $imagePath, $fileName); } public function uploadFiles($files, $place) { if(is_array($files) && !array_key_exists('tmp_name', $files)){ return array_filter(array_map(function($file) use ($place){ return $this->upload($file, $place); }, $files)); }else { return $this->upload($files, $place); } } }