image - Intervention\Image\Exception\ImageNotWritableException 使用 Laravel 4

标签 image upload laravel

我正在使用 Laravel 4。当一切似乎都正确时,我不确定为什么会出现此错误。此外,该产品不会更新到数据库。

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



的片段产品 Controller 创建产品对象的地方:
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();
            });
        }

最佳答案

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

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

关于image - Intervention\Image\Exception\ImageNotWritableException 使用 Laravel 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22876660/

相关文章:

php - 在 Laravel 中自定义模型存储的验证规则

php - 自定义 Laravel 身份验证中间件

Laravel mongodb 事务不回滚

css - 如何动态创建图像的拼贴画(没有间隙的网格),其中图像具有不同的高度?

ios - 将图像上传到服务器( Objective-C )

java - 如何将图像序列转换为 QuickTime 影片?

image - Laravel 图片上传创建文件夹而不是文件

PHP 错误 'Array' 中的未知列 'field list' 和警告 : mysql_query() expects parameter 1 to be string?

php - 从缓存中获取图像 MAGENTO

javascript - JavaScript 中鼠标光标周围的图像叠加