Created
March 23, 2017 19:42
-
-
Save vaporic/3a1f36c619f594d441396ed42faa5fee to your computer and use it in GitHub Desktop.
Upload File Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function uploadFile(Request $request) | |
{ | |
$file = $request->file; | |
$extension = $file->getClientOriginalExtension(); | |
$file_name = md5(time()).".".$extension; | |
$id_user = $request->id; | |
if(!empty($file)) { | |
// Update image DB | |
$user = Users::find($id_user); | |
$user->avatar = $file_name; | |
$user->save(); | |
// Save image original | |
$destinationPath = public_path() . '/uploads/'; | |
$success = $file->move($destinationPath, $file_name); | |
// Resize | |
$sizes = [ | |
[ | |
"width" => "48", | |
"height" => "48" | |
], | |
[ | |
"width" => "128", | |
"height" => "128" | |
], | |
[ | |
"width" => "256", | |
"height" => "256" | |
] | |
]; | |
foreach($sizes as $size){ | |
$destination = public_path() . '/uploads/'.$size['width'].'_'.$size['height'].'/'; | |
if (!is_dir($destination)) { | |
// dir doesn't exist, make it | |
mkdir($destination, 0755, true); | |
} | |
// THEN RESIZE IT | |
$returnData = $destinationPath.$file_name; | |
$image = Image::make(file_get_contents($returnData)); | |
$image = $image->resize($size['width'],$size['height'])->save($destination.$file_name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment