jeudi 11 avril 2019

How to upload Image in Database in Laravel 5.7?

I'm making an app in Laravel 5.7 . I want to upload image in database through it and I want to show it from database.

I have tried different methods around the Internet as I was getting issues in

Intervention\Image\Facades\Image  

I followed many advices from Internet make changes in config.app made changes in Composer

At the end used

use Intervention\Image\Facades\Image as Image;

So I get resolved from issue "Undefined class Image" but now I' m getting issues as "Undefined class File", Method getClientOriginalExtension not found. Method Upsize, make not found.

My code is

<?php


namespace App\Http\Controllers;
use File;
use Intervention\Image\Facades\Image as Image;
use App\User;
use Illuminate\Http\Request;


class UserController extends Controller

{
//

protected $user;

/**
 * [__construct description]
 * @param Photo $photo [description]
 */
public function __construct(
    User $user )
{
    $this->user = $user;
}

/**
 * Display photo input and recent images
 * @return view [description]
 */
public function index()
{
    $users = User::all();
    return view('profile', compact('users'));
}

public function uploadImage(Request $request)
{
    $request->validate([
        'image' => 'required',
        'image.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);

    //check if image exist
    if ($request->hasFile('image')) {
        $images = $request->file('image');

        //setting flag for condition
        $org_img = $thm_img = true;

        // create new directory for uploading image if doesn't exist
        if( ! File::exists('images/originals/')) {
            $org_img = File::makeDirectory('images/originals/', 0777, true);
        }
        if ( ! File::exists('images/thumbnails/')) {
            $thm_img = File::makeDirectory('images/thumbnails', 0777, true);
        }

        // loop through each image to save and upload
        foreach($images as $key => $image) {
            //create new instance of Photo class
            $newPhoto = new $this->user;
            //get file name of image  and concatenate with 4 random integer for unique
            $filename = rand(1111,9999).time().'.'.$image->getClientOriginalExtension();
            //path of image for upload
            $org_path = 'images/originals/' . $filename;
            $thm_path = 'images/thumbnails/' . $filename;

            $newPhoto->image     = 'images/originals/'.$filename;
            $newPhoto->thumbnail = 'images/thumbnails/'.$filename;

            //don't upload file when unable to save name to database
            if ( ! $newPhoto->save()) {
                return false;
            }

            // upload image to server
            if (($org_img && $thm_img) == true) {
                Image::make($image)->fit(900, 500, function ($constraint) {
                    $constraint->upsize();
                })->save($org_path);
                Image::make($image)->fit(270, 160, function ($constraint) {
                    $constraint->upsize();
                })->save($thm_path);
            }
        }
    }

    return redirect()->action('UserController@index');

}

}

Please suggest me any Image Upload code without updating repositories or suggest me how can I remove issues from this code.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire