使用Laravel5.1的图片上传与剪切
1.composer require intervention/image
创建一个控制器ImageController
路由:Route::get('shangchuan','ImageController@get');
Route::post('shangchuan','ImageController@post');
2.修改 app/config/app.php
添加 ServiceProvider:
// 将下面代码添加到 providers 数组中
'providers' => [
// ...
Intervention\Image\ImageServiceProvider::class,
// ...
],
// 将下面代码添加到 aliases 数组中
'aliases' => [
// ...
'Image' => Intervention\Image\Facades\Image::class,
// ...
],
3.在控制器中写:
use Intervention\Image\ImageManager;
use Storage;
然后写方法
public function get(){
return view('shangchuan');
}
public function post(Request $req){
$manager = new ImageManager(array('driver' => 'gd'));
$file = $req->file('file');
$name = $file->getClientOriginalName(); //获取文件原名
$ext = $file->getClientOriginalExtension(); // 扩展名
$realPath = $file->getRealPath(); //临时文件的绝对路径
$type = $file->getClientMimeType(); // 获取文件类型
$filename = date('YmdHis') . uniqid() . '.' . $ext; //保存图片
$bool = Storage::disk('local')->put($filename, file_get_contents($realPath)); //创建一个实例,然后亚索图片,最后保存
$image = $manager->make("image/$filename")->resize(100, 100)->save("images/$filename"); }