php 干预 / 图片上传错误 {{ 图片来源不可读 }}

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36157824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:43:52  来源:igfitidea点击:

Intervention / Image Upload Error {{ Image source not readable }}

phpimagefile-uploadlaravel-5.1intervention

提问by arispapapro

I am trying to add a profile image upload in Laravel 5.1. I used the Intervention/ImagePackage but when I try to upload the image I get this error:

我正在尝试在Laravel 5.1 中添加个人资料图片上传。我使用了Intervention/Image包,但是当我尝试上传图像时出现此错误:

NotReadableException in AbstractDecoder.php line 302: Image source not readable

AbstractDecoder.php 第 302 行中的 NotReadableException:图像源不可读

This is my PhotoController:

这是我的 PhotoController:

<?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) {}
}

This is my html form:

这是我的 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>

采纳答案by Thiago Alves

Add the following parameter in your form tag:

在表单标签中添加以下参数:

enctype="multipart/form-data"

And change for this in make:

并为此进行更改:

$img = Image::make($request->file('photo')->getRealPath());

回答by TechPotter

Try using laravel collective, Make sure you have checked files to true if you are using laravel collective

尝试使用 laravel 集合,如果您使用的是 laravel 集合,请确保已将文件检查为 true

{{ Form::open(['route'=>'your-route', 'class'=>'form',**'files'=>true**]) }} 
{{ Form::close()}}`

, then when requesting your file in store function add the getRealPath() like this Image::make(Input::file('artist_pic')->getRealPath())->resize(120,75);

,然后在 store 函数中请求您的文件时,像这样添加 getRealPath() Image::make(Input::file('artist_pic')->getRealPath())->resize(120,75);

回答by Mayeenul Islam

This is not to answer the OP's question, but to answer someone else's who is here with the same question with a different context.

这不是为了回答 OP 的问题,而是为了回答其他人的问题,他们在不同的上下文中提出了相同的问题。

If you are using File System method like storeAs()then you are actually uploading your file under /storage/app/public/from /public/scope. If so, you will need to run the following command:

如果您使用的是文件系统方法,storeAs()那么您实际上是在/storage/app/public/from/public/范围内上传文件。如果是这样,您将需要运行以下命令:

php artisan storage:link

This command will create a shortcut link to the /storagedirectory under /publicdirectory, and things should work normal.

该命令将创建一个指向/storage目录下/public目录的快捷方式链接,一切正常。