You can make a trait, and use the trait where needed. I'd have a method in the trait that would accept the name of the file input so that isn't hardcoded.
something like:
trait UserUpload
{
public function saveAndResize($inputName) {
$file_basename = time();
$file_extension = $request->$inputName->extension();
$user_id = '234567890'; // test purposes
$filepath = 'user/' . $user_id . '/uploads/';
$small = Image::make( $request->$inputName )->resize(200, null, function($constraint){
$constraint->aspectRatio();
})->stream();
$medium = Image::make( $request->$inputName )->resize(800, null, function($constraint){
$constraint->aspectRatio();
})->stream();
$large = Image::make( $request->$inputName )->resize(1000, null, function($constraint){
$constraint->aspectRatio();
})->stream();
Storage::disk('public')->put( $filepath.$file_basename.'_small.'.$file_extension, $small, 'public');
Storage::disk('public')->put( $filepath.$file_basename.'_medium.'.$file_extension, $medium, 'public');
}
}
use App\UserUpload;
class PortraitController extends Controller
{
public function store(Request $request)
{
use UserUpload;
$this->validate($request, [
'user_portrait' => 'required|mimes:jpeg,png|dimensions:min_width=800,max_width:1800|max:2048'
]);
$this->saveAndResize('user_portrait');
return back();
}
}
I'd do a bit more like return if it was successful, etc.