Laravel 5.2.x - 干预/图像 - 在null上调用成员函数encode()

时间:2015-12-26 10:36:39

标签: laravel intervention laravel-5.2

我已经创建了一个存储库来使用Intervention/Image库来管理上传图像,但是当我尝试保存图像时,我得到了Call to a member function encode() on null

这是我的界面:

namespace App\Repositories\MeetMount\ImageUploader;

use Illuminate\Http\Request;

interface ImageContract {
    public function getExt($filename);
    public function setFolder($folderName);
    public function getRandomFilename($fileName);
    public function addWatermark($watermarkName);
    public function resize($width, $height);
    public function getFileName();
    public function save(Request $file);
}

这是我的具体课程:

namespace App\Repositories\MeetMount\ImageUploader;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Intervention\Image\Image;

class InterventionImageRepository implements ImageContract {

    /**
     * @var Image
     */
    private $image;

    /**
     * @var $folder
     */
    private $folder;

    /**
     * InterventionImageRepository constructor.
     *
     * @param Image $image
     */
    public function __construct( Image $image )
    {
        $this->image = $image;
    }

    /**
     * Restituisce l'estensione dell'immagine
     *
     * @param $fileName
     * @return string
     */
    public function getExt( $fileName )
    {
        $pos = strrpos( $fileName, '.' );
        $ext = substr( $fileName, $pos, strlen( $fileName ) );

        return $ext;
    }

    /**
     * Imposta la cartella dove salvare l'immagine
     *
     * @param $folderName
     * @return $this
     */
    public function setFolder( $folderName )
    {
        $folderPath = public_path( $folderName );

        if( ! File::exists( $folderPath ) )
        {
            File::makeDirectory( $folderPath );
        }

        $this->folder = $folderPath;

        return $this;
    }

    public function getRandomFilename( $fileName )
    {
        // TODO: Implement getRandomFilename() method.
    }

    /**
     * Aggiungi watermark
     *
     * @param $watermarkName
     * @return $this
     */
    public function addWatermark( $watermarkName )
    {
        $watermarkPath = public_path( 'images/watermarks/' . $watermarkName );
        $this->image->insert( $watermarkPath );

        return $this;
    }

    /**
     * Salva il file
     *
     * @param $file
     * @return $this
     */
    public function save( Request $request )
    {
        $file = $request->file('immagine');

        $fileFolder = $this->folder . '/' . $file->getClientOriginalName();

        $this->image->save( $fileFolder );

        return $this;

    }

    public function getFileName()
    {
        // TODO: Implement getFileName() method.
    }

    public function resize( $width, $height )
    {
        // TODO: Implement resize() method.
    }
}

这是商店方法:

public function store( CreatePlaceRequest $request )
{
    $this->imageRepository->setFolder('cartella-prova')->save($request);
}

当我提交表格时,我得到:

  

Image.php第119行中的FatalErrorException:       在null上调用成员函数encode()       在Image.php第119行       在HandleExceptions.php第133行的FatalErrorException-> __ construct()中       在HandleExceptions-> HandleExceptions.php第118行中的fatalExceptionFromError()       在HandleExceptions-> HandleExceptions.php第0行中的handleShutdown()       at Image-> Image.php第139行中的encode()       at Image->在InterventionImageRepository.php第96行中保存()       在PlacesController.php第80行的InterventionImageRepository-> save()中       在PlacesController-> store()在Controller.php第76行

问题应该在这里:at Image->save() in InterventionImageRepository.php line 96

并且该行在具体类中,在save方法中:

$this->image->save( $fileFolder );

有人可以帮我找到问题吗?

1 个答案:

答案 0 :(得分:0)

根据错误:

  

在null ...上调用成员函数encode()

这意味着您尝试在未收到图像的情况下保存图像 - null

问题在于save课程中的InterventionImageRepository功能。我已修改它以检查是否有图像。

还要考虑您的图片归档名称是否有拼写错误(immagine)。确保在表单输入feild中使用type = file的相同单词。否则,将其更正为controller/class个文件和frond end views中的相同名称。还要确保使用enctype =&#34; multipart / form-data&#34;在您的<form>标记中。 <form method="post" action="target-path" enctype="multipart/form-data">

否则文件将不会与表单值一起发送到服务器。

/**
 * Salva il file
 *
 * @param $file
 * @return $this
 */
public function save( Request $request )
{
    //first check if the file received
    if($request->hasFile('immagine')){
        $file = $request->file('immagine');
        $fileFolder = $this->folder . '/' . $file->getClientOriginalName();
        $this->image->save( $fileFolder );

        return $this;
    }else{
        //Do some necessary outputs here
        //may be store a message to session and display it to user in the view
        //or return a boolean value and check for it in your store method
    }
}

请参阅此Laravel的官方文档并改进您的脚本:https://laravel.com/docs/5.2/requests#files