LARAVEL - 显示存储文件夹中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43296197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:45:17  来源:igfitidea点击:

LARAVEL - Displaying images from the storage folder

phpimagelaraveluploadcontrollers

提问by cheesycoder

I'm new to Laravel and was wondering if someone can help me out with a simple image upload.

我是 Laravel 的新手,想知道是否有人可以通过简单的图片上传来帮助我。

I have a form that allows a user to create a profile and upload and avatar for their profile during the process. This all works perfectly. Here is the code from my controller:

我有一个表单,允许用户在此过程中创建个人资料并为他们的个人资料上传和头像。这一切都完美无缺。这是我的控制器的代码:

if (request()->hasFile('avatar')) {

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

        $file->storeAs('avatars/' . auth()->id(), 'avatar.jpg');
    }

The image is saved within storage/app/avatars/USER-ID/avatar.jpg

图片保存在 storage/app/avatars/USER-ID/avatar.jpg

I'm not sure how to display the image from this folder. I've looked for solutions but I am unable to use php artisan storage:linkas it is not my server. How do I go about this without using the storage link? Can I create a link manually?

我不确定如何显示此文件夹中的图像。我一直在寻找解决方案,但我无法使用,php artisan storage:link因为它不是我的服务器。如何在不使用存储链接的情况下解决此问题?我可以手动创建链接吗?

If someone could explain a solution to me that would be perfect!

如果有人能向我解释一个解决方案,那将是完美的!

Just ask if you need any code snippets.

只需询问您是否需要任何代码片段。

Thanks!

谢谢!

采纳答案by jeremykenedy

You will need to access the protected files using a route the plugs into a controller that can access the files and pass it to the view.

您将需要使用插入到控制器的路由来访问受保护的文件,该控制器可以访问文件并将其传递给视图。

I would also recommend using the package intervention/imageit can be installed by

我还建议使用intervention/image它可以安装的包

composer require intervention/image

composer require intervention/image

See below to on accessing the image:

有关访问图像的信息,请参见下文:

// Your route in web.php

// 你的路径 web.php

// Public route to show user avatar
Route::get('avatars/{id}/{image}', [
    'uses'      => 'TheNameOfYourController@userProfileAvatar'
]);

// In your controller file. In this example according the name of what we have above it would be userProfileAvatar()

// 在你的控制器文件中。在这个例子中,根据我们上面的名称,它将是userProfileAvatar()

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Image;

class TheNameOfYourController extends Controller
{

    /**
     * Show user avatar
     *
     * @param $id
     * @param $image
     * @return string
     */
    public function userProfileAvatar($id, $image)
    {
        return Image::make(storage_path() . '/avatars/' . $id . '/' . $image)->response();
    }

}

// Your blade view to display the image

// 显示图像的刀片视图

<img src="images/profile/{{ \Auth::user()->id }}/avatar.jpg" alt="{{ \Auth::user()->name }}">

Here are some references but this intances that go one step further and save the file path the database upon upload:

以下是一些参考资料,但此实例更进一步,并在上传时将文件路径保存到数据库:

// Route Example

// 路由示例

// Controller Example

// 控制器示例

// View Examples

// 查看示例

回答by Elie Andraos

Well if you don't have access to the server to create a symlink, you need to create a route that returns the image as response.

好吧,如果您无权访问服务器来创建符号链接,则需要创建一个将图像作为响应返回的路由。

Something like that:

类似的东西:

Route::get('storage/{filename}', function ($filename) 
{
   $path = storage_path('avatars/' . $filename);

   if (!File::exists($path)) {
      abort(404);
   }

   $file = File::get($path);
   $type = File::mimeType($path);

   $response = Response::make($file, 200);
   $response->header("Content-Type", $type);

   return $response;
});

回答by Alexey Mezenin

Create a symbolic link which will connect storage/appand public/imgdirectories, for example:

创建将连接storage/apppublic/img目录的符号链接,例如:

ln -s /home/laravel/public/img /home/laravel/storage/app

Then just use asset()helper to generate a link:

然后只需使用asset()helper 生成链接:

<img src="{{ asset('img/avatars/'.auth()->id().'/avatar.jpg') }}">