mardi 17 janvier 2017

Upload an image and create its thumbnail Laravel 5.2

So yesterday I tried to make an upload file function , for when user makes his products, he can also upload a picture too.

But the picture was too big when I was iterating through the items, so I decided to use intervention package to resize the picture and also create a thumbnail picture.

I made the function but its partially working.

if($file = $request->hasFile('image')) {
        $file = $request->file('image');
        $extension = $file->getClientOriginalName();
        $username = Auth::user()->username;
        $destinationPath = public_path('/uploads/products/' . $username);
        $thumb = Image::make($file->getRealPath())->resize(100, 100, function ($constraint) {
            $constraint->aspectRatio(); //maintain image ratio
        });
        $thumb->save($destinationPath.'/thumb_'.$extension);
        $destinationPath = public_path('/uploads/products/' . $username);
        $file->move($destinationPath, $extension);
        $product['imagePath'] = '/uploads/products/'. $username . '/' . $extension;
        $product['thumbnail'] = '/uploads/products/'. $username . '/thumb_' . $extension;
    }

I made it so, different user will create a different file in /uploads/products.

Also I upload the original picture and the resized so the I should have like: picture.jpg and thumb_picture.jpg.

When the custom file is not created (from the name of the user) I get this error:

Can't write image data to path (C:\xampp\htdocs\shop\public/uploads/products/book/thumb_Jellyfish.jpg)

When I comment 6,7,8 lines, the function works but it uploads only the original picture as it supposed to. If I remove the comment, the thumbnail works too!

So I guess, after the custom folder has been created, the whole function works fine, but before it has a writable problem.

Any ideas? Everything will be appreciated!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2jmwtjm
via IFTTT

Aucun commentaire:

Enregistrer un commentaire