Skip to content

Instantly share code, notes, and snippets.

@garyconstable
Last active September 4, 2019 11:01
Show Gist options
  • Save garyconstable/3f280f87f7b8667e4e4cae2dd0487548 to your computer and use it in GitHub Desktop.
Save garyconstable/3f280f87f7b8667e4e4cae2dd0487548 to your computer and use it in GitHub Desktop.
Laravel upload an image
<?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