我正试图在Laravel 5.1中添加一个上传的配置文件图像。我使用了Intervention/Image
包,但当我尝试上传图像时,我收到了以下错误:
AbstractDecoder.php第302行出现NotReadableException:图像源不可读
这是我的照片控制器:
<?php
namespace App'Http'Controllers;
use Illuminate'Http'Request;
use Image;
use Input;
use App'Http'Requests;
use App'Http'Controllers'Controller;
class PhotoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return 'Illuminate'Http'Response
*/
public function index() {}
/**
* Show the form for creating a new resource.
*
* @return 'Illuminate'Http'Response
*/
public function create() {}
/**
* Store a newly created resource in storage.
*
* @param 'Illuminate'Http'Request $request
* @return 'Illuminate'Http'Response
*/
public function store(Request $request)
{
$img = Image::make($request->file('photo'));
$img->save('image.png');
}
/**
* Display the specified resource.
*
* @param int $id
* @return 'Illuminate'Http'Response
*/
public function show($id) {}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return 'Illuminate'Http'Response
*/
public function edit($id) {}
/**
* Update the specified resource in storage.
*
* @param 'Illuminate'Http'Request $request
* @param int $id
* @return 'Illuminate'Http'Response
*/
public function update(Request $request, $id) {}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return 'Illuminate'Http'Response
*/
public function destroy($id) {}
}
这是我的html表单:
<header>
<div class="student_profile_sub_header w100">
<div class="container ccenter">
<div class="student_profile_name">
<h4>{{$student->name}} {{$student->surname}}</h4>
</div>
<div class="student_profile_image">
<img src="{{asset('assets/profile_image.png')}}">
</div>
<form method="POST" action="../student/profile/imageupload">
{!! csrf_field() !!}
<input type="file" name="photo">
<input type="submit" value="Upload Image" name="submit">
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</form>
</div>
</div>
</header>
在表单标记中添加以下参数:
enctype="multipart/form-data"
对此进行更改:
$img = Image::make($request->file('photo')->getRealPath());
这不是为了回答OP的问题,而是为了回答其他人在不同上下文中提出的相同问题。
如果您使用像storeAs()
这样的文件系统方法,那么您实际上是在/public/
作用域的/storage/app/public/
下上传文件。如果是这样,您将需要运行以下命令:
php artisan storage:link
这个命令将在/public
目录下创建一个指向/storage
目录的快捷链接,一切正常。
奖金
我遇到了这样一种情况,我没有任何SSH访问服务器,也无法将符号链接上传到cPanel,我制作了一个像下面这样的临时路由,并点击了它一次:
Route::get('/symlink', function() {
Artisan::call('storage:link');
echo "Storage Symbolink link is created<br>";
});
完成后,我删除了路线。:)
尝试使用laravel collective,如果使用laraver collective ,请确保已将文件检查为true
{{ Form::open(['route'=>'your-route', 'class'=>'form',**'files'=>true**]) }}
{{ Form::close()}}`
,然后在请求存储中的文件函数时,添加getRealPath(),如下Image::make(Input::file('artist_pic')->getRealPath())->resize(120,75);
$filenamewithextension = $picture->getClientOriginalName();
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
$extension = $picture->getClientOriginalExtension();
$thumbnail = $filename . '_thumbnail_' . time() . '.' . $extension;
$original_picture = $filename . '_original_' . time() . '.' . $extension;
$picture->storeAs('public/profile_images', $original_picture);
$picture->storeAs('public/profile_images/thumbnail', $thumbnail);
$thumbnailpath = public_path('storage/profile_images/thumbnail/' . $thumbnail);
$this->createThumbnail($thumbnailpath, 150, 93);
$picture_path = public_path('storage/profile_images/' . $original_picture);
$this->createThumbnail($picture_path, 550, 340);
$profile_pic = new Profile_Picture();
$profile_pic->user_id = Auth::user()->id;
$profile_pic->picture_path = $original_picture;
$profile_pic->thumbnail = $thumbnail;
$profile_pic->default = 0;
$profile_pic->isVerified = 1;
$profile_pic->save();
还要检查相应的路径是否正确,即存储图像和从中获取图像的图像路径是simmilar
在我的情况下,在放入laravel干预代码时,图像不存在于其路径中。请确保您的图像存在于该路径中。
这是
$img=Image::make('photo')->resize(300, 200);
我把它改成
$img=Image::make($request->file('photo'))->resize(300, 200);
为我工作。