Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Controller
- public function store(ClientRequest $request, ClientRepositoryInterface $repository)
- {
- $request->hasFile('image');
- $file = $request->file('image');
- $filename = \str_slug($request->get('name'),'-') . '.' . $file->getClientOriginalExtension();
- $path = public_path('/uploads/clients/');
- $url = '/uploads/clients/'.$filename;
- $file->move($path, $filename);
- $image = Image::make(sprintf($path.'%s', $filename))->resize(250, 100)->save();
- return $repository->create($request->all());
- }
- // Repository
- <?php
- namespace App\Repositories;
- use Illuminate\Support\Facades\File;
- use Image;
- class ImageRepository
- {
- public function saveImage($image, $nameCli, $type, $size)
- {
- if (!is_null($image)) {
- $file = $image;
- $extension = $image->getClientOriginalExtension();
- $fileName = \str_slug($nameCli, '-') .'.' . $extension;
- $destinationPath = public_path('uploads/'.$type.'/');
- $url = 'http://'.$_SERVER['HTTP_HOST'].'/uploads/'.$type.'/'.$fileName;
- $fullPath = $destinationPath.$fileName;
- $image = Image::make($file)
- ->resize($size, null, function ($constraint) {
- $constraint->aspectRatio();
- })
- ->encode('jpg');
- $image->save($fullPath, 100);
- } else {
- return 'http://'.$_SERVER['HTTP_HOST'].'/uploads/'.$type.'/placeholder300x300.jpg';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement