如何在Laravel 5.7中上传数据库中的图片?

时间:2019-04-11 15:23:04

标签: php database laravel image laravel-5

我正在用Laravel 5.7开发一个应用程序。我想通过它上传数据库中的图像,并希望从数据库中显示它。

在遇到问题时,我尝试了各种Internet上的方法

Intervention\Image\Facades\Image  

我遵循了互联网上的许多建议,在 config.app 中进行了更改 在 Composer

中进行了更改

最后用完

use Intervention\Image\Facades\Image as Image;

所以我从问题“ Undefined class Image”中得到了解决 但是现在我遇到了“未定义的类文件”问题, 找不到方法getClientOriginalExtension。 方法升级,找不到。

我的代码是

<?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');

}

}

请向我建议任何不更新存储库的图像上传代码,或者建议我如何从此代码中删除问题。

2 个答案:

答案 0 :(得分:0)

时间的开始阅读下面的链接,因为laravel处理了创建目录和哈希图像并放置目录 laravel file system 然后在存储在目录中时读取文件名,并在需要图像检索名称字段并在服务器上调用物理地址时在表字段中保留名称

答案 1 :(得分:0)

$upload_id = $request->file('FILENAME');
$file_name = time().$upload_id->getClientOriginalName();
$destination = 
$_SERVER["DOCUMENT_ROOT"].'/adminbusinessplus/storage/uploads';
$request->file('FILENAME')->move($destination, $file_name);


$string="123456stringsawexs";
$extension = pathinfo($upload_id, PATHINFO_EXTENSION);
$path = $destination.'/'.$file_name;
$public =1;
$user_id = $request->logedin_user_id;
$hash = str_shuffle($string);

$request->user_id = $request->logedin_user_id;
$request->name = $file_name;
$request->extension = $extension;
$request->path = $path;
$request->public = $public;
$request->hash = $hash;

//$request INSERT INTO MODEL uploads
$file_id = Module::insert("uploads", $request);