【问题标题】:Intervention / Image Upload Error {{ Image source not readable }}干预/图像上传错误{{图像源不可读}}
【发布时间】:2016-07-09 13:01:50
【问题描述】:

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

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

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

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

【问题讨论】:

  • 在保存方法中提供你的目标文件路径

标签: php image file-upload laravel-5.1 intervention


【解决方案1】:

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

enctype="multipart/form-data"

并在 make 中对此进行更改:

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

【讨论】:

  • 非常感谢蒂亚戈·阿尔维斯。
【解决方案2】:

这不是要回答 OP 的问题,而是要回答其他人在不同背景下提出相同问题的问题。

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

php artisan storage:link

该命令会在/public目录下创建/storage目录的快捷链接,一切正常。

【讨论】:

  • 非常感谢这个有用且真正有指导意义的答案
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 2017-06-09
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2016-12-19
    • 2018-05-20
    • 2015-10-21
    • 2021-07-09
    相关资源
    最近更新 更多