Laravel上传产品图片Uploading img

  这节我们讲Laravel产品图片上传,有很多方式可以实现,这里我们用intervention/image插件来进行。首先安装intervention/image插件,在命令行输入

composer require intervention/image

  安装完成后要修改config/app.php文件

//在$providers中添加一行
Intervention\Image\ImageServiceProvider::class,
//在$aliases中添加一行
'Image' => Intervention\Image\Facades\Image::class,

  发布配置,在命令行中输入

php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"

  这时弹出Copied File [/vendor/intervention/image/src/config/config.php] To [/config/image.php]提示已经复制配置文件到config/image.php,你可以在这里进行设置。

  修改controller配置,文件在/app/Http/Controllers/ItemController.php,

if($request->hasFile('img'))
        {            
            $image = $request->file('img');         
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $location = public_path('img/' . $filename);
            Image::make($image)->save($location);
            $item->img = url('img/' . $filename);
        }

  修改create.blade.php文件,表格form要加一个参数enctype="multipart/form-data",选择图片改为<input type="file" name="img" >

@extends('layouts.app')

@if ($errors->any())
    <div class="alert alert-danger">
    	<strong>Errors:</strong>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

@section('content')
	<div class="container">
		<div class="row">
			<div class="col-md-8 col-md-offset-2">
				<div class="card card-default">
					<div class="card-header">Create New Item</div>
					<div class="card-body">
						<form method="POST" action="/items" aria-label="Register" enctype="multipart/form-data">
							@csrf
							<div class="form-group row">
								<label for="name" class="col-md-4 col-form-label text-md-right">Name</label>
								<div class="col-md-6">
									<input id="name" type="text" name="name" value="" required="required" autofocus="autofocus" class="form-control">
								</div>
							</div>
							<div class="form-group row">
								<label for="email" class="col-md-4 col-form-label text-md-right">Price</label>
								<div class="col-md-6">
									<input id="email" type="text" name="price" value="" required="required" class="form-control">
								</div>
							</div>
							<div class="form-group row">
								<label for="password" class="col-md-4 col-form-label text-md-right">Img</label>
								<div class="col-md-6">
									<input type="file" name="img" >
								</div>
							</div>
							<div class="form-group row">
								<label for="password-confirm" class="col-md-4 col-form-label text-md-right">Description</label>
								<div class="col-md-6">
									<input id="password-confirm" type="text" name="description" required="required" class="form-control">
								</div>
							</div>
							<div class="form-group row mb-0">
								<div class="col-md-6 offset-md-4">
									<button type="submit" class="btn btn-primary">Save</button>
								</div>
							</div>
						</form>
					</div>
				</div>	
			</div>	
		</div>	
	</div>	
@endsection

  

posted @ 2018-07-11 14:12  ytkah  阅读(325)  评论(0编辑  收藏  举报
网址导航 gg N / G Sitemap

部分内容来源于网络,如有版权问题请联系删除