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

xbp102n0  于 2022-11-21  发布在  PHP
关注(0)|答案(6)|浏览(116)

我正在尝试在Laravel 5.1中添加个人资料图片上传。我使用了Intervention/Image软件包,但当我尝试上传图片时,我收到此错误:
在第302行出现异常:图像源不可读

这是我的光控制器:

<?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>
qmelpv7a

qmelpv7a1#

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

enctype="multipart/form-data"

并为此在make中进行更改:

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

mhd8tkvw2#

这不是回答楼主的问题,而是回答其他人的问题,他们在不同的背景下提出了同样的问题。
如果您使用文件系统方法(如storeAs()),则您实际上是在/public/范围中的/storage/app/public/下上载文件。如果是这样,则需要运行以下命令:

php artisan storage:link

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

jaxagkaj

jaxagkaj3#

尝试使用laravel collective,如果您正在使用laravel collective,请确保您已将文件选中为true

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

,然后在请求存储函数中的文件时添加getRealPath(),如下所示Image::make(Input::file('artist_pic')->getRealPath())->resize(120,75);

ni65a41a

ni65a41a4#

这是$img=图像::使('照片')-〉调整大小(300,200);
我把它改成了$img=Image::make($request-〉file('photo'))-〉resize(300,200);
为我工作。

nafvub8i

nafvub8i5#

$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();

还要检查相应的路径是否正确,即存储图像的图像路径和从中获取图像的图像路径是否相似

w80xi6nr

w80xi6nr6#

在我的例子中,图像并不存在于它的路径中,而把laravel干预代码。2确保你的图像存在于那个路径中。

相关问题