Agil
Agil

Reputation: 396

Cropping image with intervention

I'm trying to crop the image before or after it's uploaded, I didn't have any luck so far. before we start, I should say I have no experience with this stuff. here is the code I tried and didn't work:

$file->move('storage/profil', $fileNameToStore)->crop(100, 100, 25, 25);

I do not understand how this thing should work but tried some basic methods to work it around. there are examples in the website like:

$img = Image::make('public/foo.jpg');
$img->crop(100, 100, 25, 25);

But when I try it, it eather does not find make or crop.

use Intervention\Image\Facades\Image;

is included at the top. I think this shouldn't be an issue. any help is appreciated in advance.

Here is the controller if you need it:

$file = $request->file('userImg');

$extension = $request->file('userImg')->getClientOriginalExtension();
$fileNameToStore = uniqid() . '.' . $extension;

$file->move('storage/profil', $fileNameToStore);

$user->userImg = $fileNameToStore;

//    $img = Image::make('public/foo.jpg');
//    $img->crop(100, 100, 25, 25);

$user->update();

Laravel 5.4

Upvotes: 0

Views: 3619

Answers (1)

mafortis
mafortis

Reputation: 7128

Try this

$img->crop(100, 100)->encode('png', 25)->trim()->save(sprintf("%s/logo.png",$path));

Using trim might solve your issue .

Update:

Try this code,

if ($request->hasFile('userImg')) { $userImg = $request->file('userImg'); $filename = 'userImg' . '-' . time() . '.' . $userImg->getClientOriginalExtension(); $location = public_path('storage/profil'); $request->file('userImg')->move($location, $filename); $user->userImg = $filename; }

Upvotes: 1

Related Questions