diff --git a/app/Http/Controllers/OrganiserController.php b/app/Http/Controllers/OrganiserController.php index f2903abc..fd5b41fe 100644 --- a/app/Http/Controllers/OrganiserController.php +++ b/app/Http/Controllers/OrganiserController.php @@ -32,7 +32,9 @@ class OrganiserController extends MyBaseController * Create the organiser * * @param Request $request + * * @return \Illuminate\Http\JsonResponse + * @throws \Symfony\Component\HttpFoundation\File\Exception\FileException */ public function postCreateOrganiser(Request $request) { @@ -53,25 +55,7 @@ class OrganiserController extends MyBaseController $organiser->confirmation_key = str_random(15); if ($request->hasFile('organiser_logo')) { - $path = public_path() . '/' . config('attendize.organiser_images_path'); - $filename = 'organiser_logo-' . $organiser->id . '.' . strtolower($request->file('organiser_logo')->getClientOriginalExtension()); - - $file_full_path = $path . '/' . $filename; - - $request->file('organiser_logo')->move($path, $filename); - - $img = Image::make($file_full_path); - - $img->resize(250, 250, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - }); - - $img->save($file_full_path); - - if (file_exists($file_full_path)) { - $organiser->logo_path = config('attendize.organiser_images_path') . '/' . $filename; - } + $organiser->setLogo($request->file('organiser_logo')); } $organiser->save(); diff --git a/app/Http/Controllers/OrganiserCustomizeController.php b/app/Http/Controllers/OrganiserCustomizeController.php index dd0e96f8..5d6840a3 100644 --- a/app/Http/Controllers/OrganiserCustomizeController.php +++ b/app/Http/Controllers/OrganiserCustomizeController.php @@ -56,24 +56,7 @@ class OrganiserCustomizeController extends MyBaseController } if ($request->hasFile('organiser_logo')) { - $the_file = \File::get($request->file('organiser_logo')->getRealPath()); - $file_name = str_slug($organiser->name).'-logo-'.$organiser->id.'.'.strtolower($request->file('organiser_logo')->getClientOriginalExtension()); - - $relative_path_to_file = config('attendize.organiser_images_path').'/'.$file_name; - $full_path_to_file = public_path($relative_path_to_file); - - $img = Image::make($the_file); - - $img->resize(250, 250, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - }); - - $img->save($full_path_to_file); - - if (\Storage::put($file_name, $the_file)) { - $organiser->logo_path = $relative_path_to_file; - } + $organiser->setLogo($request->file('organiser_logo')); } $organiser->save(); diff --git a/app/Models/Organiser.php b/app/Models/Organiser.php index 61b81d0c..09c8cad2 100644 --- a/app/Models/Organiser.php +++ b/app/Models/Organiser.php @@ -2,7 +2,9 @@ namespace App\Models; +use Illuminate\Http\UploadedFile; use Str; +use Image; class Organiser extends MyBaseModel { @@ -112,4 +114,37 @@ class Organiser extends MyBaseModel public function getDailyStats() { } + + + /** + * Set a new Logo for the Organiser + * + * @param \Illuminate\Http\UploadedFile $file + */ + public function setLogo(UploadedFile $file) + { + $filename = str_slug($this->name).'-logo-'.$this->id.'.'.strtolower($file->getClientOriginalExtension()); + + // Image Directory + $imageDirectory = public_path() . '/' . config('attendize.organiser_images_path'); + + // Paths + $relativePath = config('attendize.organiser_images_path').'/'.$filename; + $absolutePath = public_path($relativePath); + + $file->move($imageDirectory, $filename); + + $img = Image::make($absolutePath); + + $img->resize(250, 250, function ($constraint) { + $constraint->aspectRatio(); + $constraint->upsize(); + }); + + $img->save($absolutePath); + + if (file_exists($absolutePath)) { + $this->logo_path = $relativePath; + } + } }