Reputation: 1549
Aloha, I have two methods in my Controller, one for setting the profile picture and the other for updating it. I'm using this lines of code in both methods:
$user = Auth::user();
if (Input::file('image')) {
$image = Image::make(Input::file('image'));
$fullName = Input::file('image')->getClientOriginalName();
$extension = Input::file('image')->getClientOriginalExtension();
$pathToCreate = public_path() .'/images/'. $user->email . '/';
$fullPath = $pathToCreate . $user->email . '.' . $extension;
$pathDatabase = 'images/' . $user->email . '/' . $user->email . '.' .$extension;
// Creating directory if it does not exists
File::exists($pathToCreate) or File::makeDirectory($pathToCreate);
$image->resize(null, 145, function ($constraint) { $constraint->aspectRatio(); })
->crop(130,130)
->save($fullPath);
$user->picture = $pathDatabase;
I want to create a method with this lines of code but I feel that the controller is not a good place for it. Where should I place this method?
Upvotes: 0
Views: 88
Reputation: 2016
Generally speaking, it depends on the purpose of the code. If this is the only place on your site that you'll be using that code, it is perfectly acceptable to include the code a private method in the Controller class.
If this code will be used in other controllers, you probably want to create a service class to handle this. This would equate to something like the following when used in your controller:
$user->picture = $this->fileUploadService->process( Input::file('image') );
The extremely short answer is: put it where it makes the most sense. A generic solution should be broadly available as a service. A specific solution should go where ever that specificity is needed (controller, repository, model, etc.).
Upvotes: 2
Reputation: 1666
You should check out Laravel-Stapler. Very useful for handling image uploads in Laravel. https://github.com/CodeSleeve/laravel-stapler
Rather then saving an image and checking if it exists you can attach (or "Staple") an image to a model.
An example taken from the docs on how to setup the form is below.
<?= Form::open(['url' => action('UsersController@store'), 'method' => 'POST', 'files' => true]) ?>
<?= Form::input('first_name') ?>
<?= Form::input('last_name') ?>
<?= Form::file('picture') ?>
<?= Form::submit('save') ?>
<?= Form::close() ?>
The model would have something like this in the controller:
$this->hasAttachedFile('picture', [
'styles' => [
'thumbnail' => '100x100',
'large' => '300x300',
'pictureCropped' => '75x75#'
],
'url' => '/system/:attachment/:id_partition/:style/:filename',
'default_url' => '/:attachment/:style/missing.jpg'
]);
Then all you do in the controller is User::create(Input::all());
Checking existence of "attachments" is as easy as if ($user->picture) ...
So the saving of the file is already taking care of by Stapler and the cropping is done automatically via the 'pictureCropped' => '75x75#'
configuration. This should remove enough of the code that you don't need to make another method.
Hope this helps!
Upvotes: 1