Edit File: Uploadable.php
<?php namespace App\Traits; use Illuminate\Support\Facades\File; use Intervention\Image\Facades\Image; use Ramsey\Uuid\Uuid; trait Uploadable { public function uploadOne($file, $directory = 'unknown',$type='image', $resize1 = null, $resize2 = null) { if (!File::isDirectory('assets/uploads/' . $directory)) { File::makeDirectory('assets/uploads/' . $directory, 0777, true, true); } if($type=='file'){ $ext = '3gp'; $name = md5($file->getClientOriginalName()) . time() . rand(99999, 1000000) . '.' . $ext; $destinationPath = public_path('assets/uploads/chat'); $imagePath = $destinationPath . "/" . $name; $file->move($destinationPath, $name); $data = ['name' => $name, 'url' => url('assets/uploads/chat' . $name)]; return $name; }else{ info($file->getClientOriginalExtension()); if (is_file($file)) { $img = Image::make($file); $thumbsPath = 'assets/uploads/' . $directory; $name = time() . '_' . rand(1111, 9999) . '.' . $file->getClientOriginalExtension(); if (null != $resize1) { $img->resize($resize1, $resize2, function ($constraint) { $constraint->aspectRatio(); }); $thumbsPath = 'assets/uploads/' . $directory; $img->save($thumbsPath . '/' . $name); } $img->save($thumbsPath . '/' . $name); return (string) $name; } else { $name = time() . rand(1000000, 9999999) . '.png'; // file_put_contents(base_path().'assets/uploads/' . $directory . '/' . $name, base64_decode($file)); $img = Image::make(base64_decode($file)); if (null != $resize) { $img->resize($resize1, $resize2, function ($constraint) { $constraint->aspectRatio(); }); $thumbsPath = 'assets/uploads/' . $directory; } $img->save($thumbsPath . '/' . $name); return (string) $name; } } } // public function uploadOne($file, $domain, $aspect = false, $width = null, $height = null) // { // $directory = public_path('assets/uploads/' . $domain); // if (!File::isDirectory($directory)) { // File::makeDirectory($directory, 0777, true, true); // } // $image = Image::make($file); // // resize image to fixed size with no aspect // if ($aspect != false and $width != null and $height != null) { // $image->resize($width, $height); // // resize only the width of the image with no aspect // } elseif ($aspect == false and $width != null) { // $image->resize($width, null); // // resize only the height of the image with no aspect // } elseif ($aspect == false and $width != null) { // $image->resize(null, $height); // // resize the image to a *fixed width* and constrain aspect ratio (auto height) // } elseif ($aspect == true and $width != null) { // $image->resize($width, null, function ($constraint) { // $constraint->aspectRatio(); // }); // // resize the image to a *fixed height* and constrain aspect ratio (auto height) // } elseif ($aspect == true and $height != null) { // $image->resize(null, $height, function ($constraint) { // $constraint->aspectRatio(); // }); // } // $path = Uuid::uuid4()->toString() . '.' . 'JPG'; // $image->save($directory . '/' . $path); // return $path; // } public function uploadFile($file, $domain) { $directory = public_path('assets/uploads/' . $domain); if (!File::isDirectory($directory)) { File::makeDirectory($directory, 0777, true, true); } $path = Uuid::uuid4()->toString() . '.' . $file->getClientOriginalExtension(); $file->move($directory, $path); return $path; } public static function uploadMultiExtension($request, $object) { foreach ($request->file as $file) { if ($file->extension() == 'pdf') { $directory = public_path('assets/uploads/' . 'documents'); if (!File::isDirectory($directory)) { File::makeDirectory($directory, 0777, true, true); } $path = Uuid::uuid4()->toString() . '.' . $file->getClientOriginalExtension(); $file->move($directory, $path); $object->documents()->create([ 'type' => 'pdf', 'path' => $path, ]); } else { $directory = public_path('assets/uploads/' . 'documents'); if (!File::isDirectory($directory)) { File::makeDirectory($directory, 0777, true, true); } $image = Image::make($file); $path = Uuid::uuid4()->toString() . '.' . $file->getClientOriginalExtension(); $image->save($directory . '/' . $path); $object->documents()->create([ 'type' => 'image', 'path' => $path, ]); } } } public function deleteFile($file_name, $directory = 'unknown'): void { if ($file_name && $file_name != 'default.png' && file_exists("assets/uploads/$directory/$file_name")) { unlink("assets/uploads/$directory/$file_name"); } } public function defaultImage($directory) { return asset("assets/uploads/$directory/default.png"); } public static function getImage($name, $directory) { return asset("assets/uploads/$directory/" . $name); } }
Back to File Manager