laravel商品图片怎么展示_我应该如何在Laravel中提供图片?

I'm storing user profile pictures in laravel storage folder instead of the public folder because I would like to keep the public folder clean from user clutter.

In order to serve an image from that folder, I created a simple Controller Action as follows:

public function profilePicture($person, $size = 40){

$profile_picture_url = storage_path().'/profile_pictures/'.$person['id'].'/profile_'.$size.'.jpg';

if(!File::exists( $profile_picture_url ))

App::abort(404);

return Image::make($profile_picture_url)->response('jpg');

}

Can this be considered a good practice, or should I simply save pictures in the public folder?

Will I run into performance issues by doing so?

解决方案

The Short Answer to your Question

Can this be considered a good practice, or should I simply save

pictures in the public folder? Will I run into performance issues by

doing so?

It is not suggested practice, because you read the file and re-generate it, that will take process time and load the server, but that said all depends on how many requests, image size, etc. I used this practice to secure/protect images/files from public access, so only Authenticated member can access the images/files as it is in this answer. Again depending on file size, number of requests and server specification, I have used it for while and I have had no issues with performance, it has worked fine (my server is 512MBMemory, 1 CoreProcessor, 20GBSSD Disk VPS solution). You might give it a try for while and see.

Symbolic link solution

It is also possible, to create symbolic link like

ln -s /pathof/laravel/storage/profile_pictures /pathof/laravel/public/profile

This solution won't affect performance, but you need to document the solution in your internal documentation, in case you move your setup to new provider or in case you need to re-link to the storage folder.

But if you still wish to have the full solution for returning image from storage folder, first of all we need to install Intervention Image for Laravel, I am not sure if this done already or not. if you have install it continue here, but if not follow the last part of this answer and than continue with Laravel solution.

Laravel solution

As said we assume your intervention works, first of all you need to create a Route. The Route will forward all image request access to our Controller.

Create Route

Route::get('profile/{person}', 'ImagesController@profilePicture');

After creating a route, we need to create a controller to take care of image requests from our route.

Create ImagesController

From command

php artisan make:controller ImagesController

And your controller should look like this.

class ImagesController extends Controller {

public function profilePicture($person, $size = 40)

{

$storagePath = storage_path('/profile_pictures/' . $person . '/profile_' . $size . '.jpg');

return Image::make($storagePath)->response();

}

}

EDIT

For those who use Laravel 5.2 and newer. Laravel introduces new and better way to serve files that has less overhead (This way does not regenerate the file as mentioned in the answer):

File Responses

The file method can be used to display a file, such as an image or

PDF, directly in the user's browser instead of initiating a download.

This method accepts the path to the file as its first argument and an

array of headers as its second argument:

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

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

And remember to add

use Intervention\Image\Facades\Image;

in your ImagesController class

Finally be sure you have created folder structure with test image.

storage/profile_pictures/person/profile_40.jpg

Now if you write in your browser

http://laravelLocalhostUrl/profile/person

It will show your image, I have made it my self and test it.

Note: I have tried best possible to make folder reflect your question,

but you can easily modify it to fit the way you want.

Install Intervention (skip this part if you already installed it)

Briefly: php composer require intervention/image

And In in your config/app the $providers array add the service providers for this package.

Intervention\Image\ImageServiceProvider::class

Add the facade of this package to the $aliases array.

'Image' => Intervention\Image\Facades\Image::class

The solution inspired from this answer but that one is to protect image with authentication in general and this answer.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值