使用Laravel 4进行干预\ Image \ Exception \ ImageNotWritableException

时间:2014-04-05 04:41:19

标签: image upload laravel

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

  

错误:干预\ Image \ Exception \ ImageNotWritableException   无法将图像数据写入路径[/img/products/1396668877.jpg]

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

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

产品对象传递到 视图

@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

产品 型号

<?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');
    }
}
数据库

中的

产品

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

2 个答案:

答案 0 :(得分:15)

确保public/img/products文件夹存在且可写,并在必要时尝试使用绝对路径,如下所示:

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

答案 1 :(得分:0)

替换:

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

使用:

Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products/'.$filename);

您必须为public方法指定save文件夹。