laravel

使用intervention给laravel上传缩略图片和水印

日期:2017-03-15 阅读:2765

dmandwp系统 - wordpress系统和DM系统区块建站>>


进入网易云课堂播放
    |    更多视频教程>

 前面讲了 laravel文件系统和图片上传的功能

这节课,我们讲下laravel使用intervention实现缩略图片和水印的功能。

http://image.intervention.io/

-------------------

安装
composer  require intervention/image

配置:
 编辑 config/app.php  

In the $providers array  :  
Intervention\Image\ImageServiceProvider::class

  $aliases array:  
'Image' => Intervention\Image\Facades\Image::class
--------------
By default Intervention Image uses PHP's GD library extension to process all images. 
If you want to switch to Imagick, you can pull a configuration file into your application by running on of the following artisan command.

Publish configuration in Laravel 5
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"

-----
使用:

// open an image file

$img = Image::make(public_path('tvupload/test1.jpg'));

// now you are able to resize the instance
$img->resize(320, 240);

// and insert a watermark for example
$img->insert(public_path('tvupload/watermark.png'),'bottom-right');

http://image.intervention.io/api/insert
The possible values are:

top-left (default)
top
top-right
left
center
right
bottom-left
bottom
bottom-right


// finally we save the image as a new file
$img->save(public_path('tvupload/test221.jpg'));

-----------
use Image;  -- 这个要记得

public function update_avatar(Request $request){
    if($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time().'.'.$avatar->getClientOriginalExtension();
            Image::make($avatar)->resize(300,300)-save(public_path('tvupload/avatars/'.$filename));
            $user = Auth::user();
            $user->avatar = $filename;
            $user->save();
    }
}

---------------------

 // Storage::disk('public')->putFileAs('product', $file,$filename); -- 这是大图片,如果下面的水印开启,则这里可以注释

Image::make($file)->resize(300,300)->save(storage_path('app/public/product/small/'.$filename));  -- 缩略图片

Image::make($file)->insert(public_path('watermark.jpg'),'top-right')->save(storage_path('app/public/product/'.$filename)); -- 水印

具体请看视频教程

 

<<点击返回