Unified Fileupload function to prevent different image and locations

This commit is contained in:
JapSeyz 2016-11-20 00:19:54 +01:00
parent 0f3737e02a
commit 7e8c785cf4
3 changed files with 39 additions and 37 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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;
}
}
}