Last active
September 4, 2019 11:01
-
-
Save garyconstable/3f280f87f7b8667e4e4cae2dd0487548 to your computer and use it in GitHub Desktop.
Laravel upload an image
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
<?php | |
function store(Request $request) | |
{ | |
$this->validateImage($request); | |
$image = new Image(); | |
$file = $request->file('file'); | |
$image->original_file_name = 'original.'.$file->extension(); | |
$image->thumbnail_file_name = 'thumbnail.'.$file->extension(); | |
$image->save(); | |
$disk = Image::disk(); | |
// Upload the original image... | |
$disk->putFileAs( | |
$image->getBasePath(), | |
$file, | |
$image->original_file_name | |
); | |
// Create and upload the thumbnail... | |
$disk->put( | |
$image->thumbnail_file_path, | |
value(function () use ($file, $image, $disk) { | |
return Manipulator::make($file)->fit(300, 300)->stream(); | |
}) | |
); | |
return new ImageResource($image); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment