从存储中显示Laravel中的图像

时间:2018-01-09 18:55:19

标签: laravel laravel-5

所以我的存储空间中有一个名为statuses的文件夹,这些文件是上传的照片,可以是私有的还是公共的,所以我无法将它们上传到'公共文件夹'

我正在做一个简单的uppload:

    // If they have uploaded image(s)
    foreach($request->photos ?? [] as $photo){

        // Store the file
        $saveFile = Storage::putFile('status/' . $status->id, $photo, 'public');

        $photoAttributes = [
            'name'      => $photo->getClientOriginalName(),  
            'extension' => $photo->getClientOriginalExtension(),  
            'path'      => $saveFile, 
            'size'      => $photo->getSize(),  
            'mime'      => $photo->getMimeType(),  
        ];

        $create = $status
            ->images()
            ->save(new $this->photos($photoAttributes));
    }

将文件上传到/ status / {statusid} / file

然后我将信息保存在数据库中,如下所示:

    Schema::create('files', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('extension');
        $table->string('path');
        $table->string('size');
        $table->string('mime');
        $table->integer('owner_id');
        $table->string('owner_type');
        $table->timestamps();
        $table->softDeletes();
    });

测试数据

INSERT INTO `social`.`files` 
    (`id`, 
    `name`, 
    `extension`, 
    `path`, 
    `size`, 
    `mime`, 
    `owner_id`, 
    `owner_type`, 
    `created_at`, 
    `updated_at`, 
    `deleted_at`) 
VALUES      
    ('2', 
    '2m2klhq1mzzz.jpg', 
    'jpg', 
    'status/2/iu4KiozJGNRdSMSju40T1UVVnop3bWZSRddXYIe8.jpeg', 
    '48033', 
    'image/jpeg', 
    '2', 
    'App\\Modules\\Timeline\\Entities\\Timeline', 
    '2018-01-09 14:54:31', 
    '2018-01-09 14:54:31', 
    NULL); 

该文件也保存在文件夹中:

enter image description here

现在,当我想显示它时,我正在使用minme类型并返回这样的文件:

/**
 * ------------------
 * File routes
 * ------------------
 */
Route::resource('photos', 'PhotosController');


/**
 * Display the specified resource.
 *
 * @param  \App\Modules\Files\Entities\Photos  $photo
 * @return \Illuminate\Http\Response
 */
public function show(Photos $photo)
{
    $this->authorize('view', $photo);

    return (new Response(Storage::get($photo->path), 200))
        ->header('Content-Type', $photo->mime);
}

当我访问该页面查看图像时,我会得到一个像这样的小白色方块:

enter image description here

当我在show方法中输入内容时的响应

https://pastebin.com/UbzJRRZv

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您使用的是哪种Laravel版本?

如果您使用> = 5.2,则可以尝试response()->file Laravel Doc

return response()->file($pathToFile, $headers);

答案 1 :(得分:0)

我这样做

我的路线

Route::get('photo/{delivery_photoEntity}/show', 'Operations\Delivery_photoController@show')->name('Delivery_photo.Show');

我的控制器

class Delivery_photoController
{
    public function show(Delivery_photoEntity $delivery_photoEntity)
    {
        return response()->file($delivery_photoEntity->path);
    }
}

我的观点

<img src="{{ route('Delivery_photo.Show', $photo) }}" style="max-width: 100%; max-height: 300px;">