-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jovert Lota Palonpon
committed
Mar 29, 2019
1 parent
a24e8b9
commit def767a
Showing
9 changed files
with
355 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Utils\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class Uploader extends Facade | ||
{ | ||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'uploader'; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace App\Utils; | ||
|
||
use Image; | ||
use Storage; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Http\UploadedFile; | ||
|
||
class Uploader | ||
{ | ||
/** | ||
* @var string The storage driver. | ||
*/ | ||
protected $disk; | ||
|
||
public function __construct() | ||
{ | ||
$this->disk = config('filesystems.default'); | ||
} | ||
|
||
/** | ||
* Specify which storage driver will be used. | ||
* | ||
* @param string $name | ||
* | ||
* @return App\Utils\Uploader | ||
*/ | ||
public function disk(string $name = 'public') | ||
{ | ||
$this->disk = $name; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Put the file into the storage. | ||
* | ||
* @param string $directory | ||
* @param Illuminate\Http\UploadedFile $file | ||
* | ||
* @return array | ||
*/ | ||
public function put(string $directory, UploadedFile $file) | ||
{ | ||
$filename = str_random(64).'.'.$file->getClientOriginalExtension(); | ||
$original_filename = $file->getClientOriginalName(); | ||
|
||
$path = Storage::disk($this->disk)->putFileAs( | ||
$directory, | ||
$file, | ||
$filename | ||
); | ||
|
||
if (Str::startsWith($file->getClientMimeType(), 'image')) { | ||
$thumbnailDirectory = "{$directory}/thumbnails"; | ||
|
||
if (! Storage::exists($thumbnailDirectory)) { | ||
Storage::makeDirectory($thumbnailDirectory); | ||
} | ||
|
||
$fileSystemRoot = config("filesystems.disks.{$this->disk}.root"); | ||
$fullPath = "{$fileSystemRoot}/{$path}"; | ||
$thumbnailPath = | ||
"{$fileSystemRoot}/{$thumbnailDirectory}/{$filename}"; | ||
|
||
Image::make($fullPath) | ||
->fit(240) | ||
->save($thumbnailPath, 95); | ||
} | ||
|
||
$path = Storage::url($path); | ||
|
||
return compact([ | ||
'directory', 'filename', 'original_filename', 'path' | ||
]); | ||
} | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.