Intervention \ Image \ Exception \ ImageNotWritableException 使用 Laravel 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22876660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intervention \ Image \ Exception \ ImageNotWritableException using Laravel 4
提问by Stephen Tafler
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.
我正在使用 Laravel 4。当一切似乎都正确时,我不确定为什么会出现此错误。此外,该产品不会更新到数据库。
Error: Intervention \ Image \ Exception \ ImageNotWritableException Can't write image data to path [/img/products/1396668877.jpg]
错误:Intervention \ Image \ Exception \ ImageNotWritableException 无法将图像数据写入路径 [/img/products/1396668877.jpg]
Snippet of ProductsControllerwhere product object is created:
创建产品对象的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();
}
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');
}
}
productstable 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();
});
}
回答by The Alpha
Make sure the public/img/products
folder exists and it's writable and also try to use absolute path if necessary, like this:
确保该public/img/products
文件夹存在且可写,并在必要时尝试使用绝对路径,如下所示:
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
回答by DevMarwen
replace :
代替 :
Image::make($image->getRealPath())->resize(468, 249)->save('/img/products/'.$filename);
with:
和:
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products/'.$filename);
you must specify the public
folder for the save
method.
您必须public
为该save
方法指定文件夹。