Skip to content

Instantly share code, notes, and snippets.

@vaporic
Created March 23, 2017 19:42
Show Gist options
  • Save vaporic/3a1f36c619f594d441396ed42faa5fee to your computer and use it in GitHub Desktop.
Save vaporic/3a1f36c619f594d441396ed42faa5fee to your computer and use it in GitHub Desktop.
Upload File Laravel
<?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