使用 Laravel 4 的干预 \ Image \ Exception \ ImageNotWritableException
Posted
技术标签:
【中文标题】使用 Laravel 4 的干预 \\ Image \\ Exception \\ ImageNotWritableException【英文标题】:Intervention \ Image \ Exception \ ImageNotWritableException using Laravel 4使用 Laravel 4 的干预 \ Image \ Exception \ ImageNotWritableException 【发布时间】:2014-05-17 14:02:54 【问题描述】:我正在使用 Laravel 4。当一切似乎都正确时,我不确定为什么会出现此错误。此外,该产品未更新到数据库。
错误:Intervention\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');
数据库中的products表
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();
);
【问题讨论】:
【参考方案1】:确保public/img/products
文件夹存在且可写,如有必要,请尝试使用绝对路径,如下所示:
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
【讨论】:
完美运行!谢谢。为什么绝对路径优于相对路径。是因为它在本地服务器上吗?【参考方案2】:替换:
Image::make($image->getRealPath())->resize(468, 249)->save('/img/products/'.$filename);
与:
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products/'.$filename);
您必须为save
方法指定public
文件夹。
【讨论】:
以上是关于使用 Laravel 4 的干预 \ Image \ Exception \ ImageNotWritableException的主要内容,如果未能解决你的问题,请参考以下文章