出现错误:AbstractDecoder.php行302中的NotReadableException [英] Getting Error: NotReadableException in AbstractDecoder.php line 302

查看:35
本文介绍了出现错误:AbstractDecoder.php行302中的NotReadableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循有关如何使用PHP,Laravel 5.1,MySQL,InterventionImage插件保存照片并将其调整为缩略图的教程,并且在尝试添加照片时出现以下错误:

I am following a turtorial on how to save photos and resize them as thumbnails using PHP, Laravel 5.1, MySQL, InterventionImage plugin, and am getting the following error when I try to add photos:

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

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

问题似乎出在下面的 makeThumbnail 方法中的照片代码中.原始照片存储在我的Flyers/照片文件夹中,但是从未创建缩略图文件,并且照片也未存储在数据库中.我已经按照所有步骤进行了检查,检查了路线,模型,视图控制器,但所有内容似乎都与本教程相同,所以我不确定自己在做什么错.如果我用dd()这个函数,错误就会停止出现,但是我经过多次尝试未能修复.

It seems that the problem is happening in the Photo code below in the makeThumbnail method. The original photo is stored in my flyers/photo folder, but the thumbnail file is never created and the photo is not stored in the database. I have followed all the steps and checked the routes, models, views controllers but everything seems to be identical to the tutorial so I am not sure what I am doing wrong.The error stops showing up if I dd() this function, but I have failed at fixing it after many attempts. The

照片雄辩模型:

<?php
namespace App;

use Intervention\Image\Facades\Image;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class Photo extends Model
{
protected $table = 'flyer_photos';
protected $fillable = ['path' , 'name' , 'thumbnail_path'];
protected $baseDir = 'flyer/photos';

public function flyer(){
    // creates a new instance of a photo
    return $this->belongsTo('App\Flyer');
}

/**
 * Build a photo instance from a file upload
 */
public static function named($name){


    return (new static)->saveAs($name);

}

/**
 * Setting name, path, and thumbnail_path parameters for Photo instance
 */
protected function saveAs($name){
    //concatenate file name with current time to prevent duplicate entries in db
    $this->name = sprintf("%s-%s", time(), $name);
    $this->path = sprintf("%/%s", $this->baseDir, $this->name);
    $this->thumbnail_path =sprintf("%s/tn-%s", $this->baseDir, $this->name);

    return $this;

}

public function move(UploadedFile $file){

    // move the file to new location in flyer/photos
    $file->move($this->baseDir, $this->name);

    $this->makeThumbnail();

    return $this;
}

/**
 * Change sizing of thumbnail and save it
 */
protected function makeThumbnail() {
    //dd('error test');
    Image::make($this->path)
    ->fit(200)
    ->save($this->thumbnail_path);
}

控制器代码:

<?php

namespace App\Http\Controllers;
use App\Flyer;
use Illuminate\Http\Request;
use App\Http\Requests\FlyerRequest;
use App\Http\Controllers\Controller;
use App\Http;
use App\Photo;
use Symfony\Component\HttpFoundation\File\UploadedFile;


class FlyersController extends Controller
{

/**
 * Auth checks to make sure you are logged in before making any adjustments
 */
public function __construct()
{
    $this->middleware('auth', ['except' => ['show']]);
}

/**
 * Display a listing of the resource.
 */
public function index()
{
    //
}

/**
 * Show the form for creating a new resource.
 */
public function create()
{
    flash()->overlay('Hello World', 'this is the message');

    return view('flyers.create');
}

/**
 * Store a newly created resource in storage.
 */
public function store(FlyerRequest $request)
{

    //persist the flyer
    Flyer::create($request->all());

    //flash messaging
    flash()->success('Success!', 'Your flyer has been created.');

    return view('pages.home');//temporary redirect the landing page

}

/**
 * Display the specified resource.
 */
public function show($zip, $street)
{
    //find the new flyer
    $flyer = Flyer::locatedAt($zip, $street);

    return view('flyers.show', compact('flyer'));

}

/**
 * Apply photo to the referenced flyer.
 */
public function addPhoto($zip, $street, Request $request){

    //confirmtion that the photo file will be in appropriate format types
    $this->validate($request, [
        'photo' => 'required|mimes:jpg,jpeg,png,bmp'
    ]);

    //build up our photo instance taking the file from dropzone plugin
    $photo = $this->makePhoto($request->file('photo'));

    //Save photo and associate it with the Flyer
    Flyer::locatedAt($zip, $street)->addPhoto($photo);
}

protected function makePhoto(UploadedFile $file){

    //get new photo object with current name
    return Photo::named($file->getClientOriginalName())->move($file);
}
}

推荐答案

似乎您的路径未在此处正确设置:

It looks like your path is not being set correctly here:

$this->path = sprintf("%/%s", $this->baseDir, $this->name);

那不是...

$this->path = sprintf("%s/%s", $this->baseDir, $this->name);

这篇关于出现错误:AbstractDecoder.php行302中的NotReadableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆