使用Laravel 4干预\ Image \ Exception \ ImageNotWritableException [英] Intervention \ Image \ Exception \ ImageNotWritableException using Laravel 4

查看:308
本文介绍了使用Laravel 4干预\ Image \ Exception \ ImageNotWritableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 4.我不知道为什么当一切看起来都正确时我会收到此错误。此外,该产品不会更新到数据库。

I am using Laravel 4. I am not sure why I am getting this error when everything seems to be correct. Also, the product is not updating to the database.


错误:干预\图像\异常\ ImageNotWritableException
无法将图像数据写入路径[/ img /产品/ 1396668877.jpg]

Error: Intervention \ Image \ Exception \ ImageNotWritableException Can't write image data to path [/img/products/1396668877.jpg]

ProductsController 的片段,其中创建了产品对象:

Snippet of ProductsController where product object is created:

public function postCreate() {
    $validator = Validator::make(Input::all(), Product::$rules);

    if ($validator->passes()) {
        $product = new Product;
        $product->category_id = Input::get('category_id');
        $product->title = Input::get('title');
        $product->description = Input::get('description');
        $product->price = Input::get('price');

        $image = Input::file('image');
        $filename  = time() . '.' . $image->getClientOriginalExtension();
        Image::make($image->getRealPath())->resize(468, 249)->save('/img/products/'.$filename);
        $product->image = 'img/products/'.$filename;
        $product->save();

        return Redirect::to('admin/products/index')
            ->with('message', 'Product Created');
    }

    return Redirect::to('admin/products/index')
        ->with('message', 'Something went wrong')
        ->withErrors($validator)
        ->withInput();
}

传递给 视图的产品对象

product object passed to view

@foreach($products as $product)
                <li>
                    {{ HTML::image($product->image, $product->title, array('width'=>'50')) }} 
                    {{ $product->title }} - 
                    {{ Form::open(array('url'=>'admin/products/destroy', 'class'=>'form-inline')) }}
                    {{ Form::hidden('id', $product->id) }}
                    {{ Form::submit('delete') }}
                    {{ Form::close() }} - 

                {{ Form::open(array('url'=>'admin/products/toggle-availability', 'class'=>'form-inline'))}}
                {{ Form::hidden('id', $product->id) }}
                {{ Form::select('availability', array('1'=>'In Stock', '0'=>'Out of Stock'), $product->availability) }}
                {{ Form::submit('Update') }}
                {{ Form::close() }}
            </li>
        @endforeach

产品 模型

Products Model

<?php

class Product extends Eloquent {

    protected $fillable = array('category_id', 'title', 'description', 'price', 'availability', 'image');

    public static $rules = array(
        'category_id'=>'required|integer',
        'title'=>'required|min:2',
        'description'=>'required|min:20',
        'price'=>'required|numeric',
        'availability'=>'integer',
        'image'=>'required|image|mimes:jpeg,jpg,bmp,png,gif'
    );

    public function category() {
        return $this->belongsTo('Category');
    }
}

产品表中的 数据库

products table in the database

    public function up()
        {
            Schema::create('products', function($table){
                $table->increments('id');
                $table->integer('category_id')->unsigned();
                $table->foreign('category_id')->references('id')->on('categories');
                $table->string('title');
                $table->text('description');
                $table->decimal('price', 6, 2);
                $table->boolean('availability')->default(1);
                $table->string('image');
                $table->timestamps();
            });
        }


推荐答案

确保 public / img / products 文件夹存在并且它是可写的,并且如果需要也尝试使用绝对路径,如下所示:

Make sure the public/img/products folder exists and it's writable and also try to use absolute path if necessary, like this:

$filename  = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);

这篇关于使用Laravel 4干预\ Image \ Exception \ ImageNotWritableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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