我使用Laravel4与Laravel-stapler来操作图像。
我在网上设计产品目录,有两个表。
并且,我将模型设置如下,
class Products extends Eloquent implements StaplerableInterface {
public function __construct(array $attributes = array()) {
$this->hasAttachedFile('product_main_image', [
'styles' => [
'thumb' => ['dimensions' => '100x100#' ],
'display' => ['dimensions' => '300x300#' ],
],
'url' => '/uploads/product/:id/:style/:filename',
]);
...
class ProductGallery extends Eloquent implements StaplerableInterface {
public function __construct(array $attributes = array()) {
$this->hasAttachedFile('product_gallery', [
'styles' => [
'thumb' => ['dimensions' => '100x100#' ],
'gallery' => ['dimensions' => '1024x' ],
],
'url' => '/uploads/gallery/:id/:style/:filename',
]);
...
当我上传产品并有四个图库文件时,它们存储如下,
/uploads/product/1/thumb/aaaa.jpg
/uploads/gallery/1/thumb/g1.jpg
/uploads/gallery/2/thumb/g2.jpg
/uploads/gallery/3/thumb/g3.jpg
/uploads/gallery/4/thumb/g4.jpg
我想将产品库的文件上传分组到产品文件夹中,例如
/uploads/product/1/thumb/aaaa.jpg
/uploads/product/1/gallery/1/thumb/g1.jpg
/uploads/product/1/gallery/2/thumb/g2.jpg
/uploads/product/1/gallery/3/thumb/g3.jpg
/uploads/product/1/gallery/4/thumb/g4.jpg
此代码在Controller中,
$model = new ProductGallery();
$model->fk_id = $product_id;
$model->image = Input::file('file');
$pathInfo = pathinfo(Input::file('file')->getClientOriginalName());
$newFilename = 'gallery-' . date("Ymd-") . sha1(time()) . '.' . $pathInfo['extension'];
$model->image->instanceWrite('file_name', $newFilename);
$model->save();
如何配置Laravel-stapler?
答案 0 :(得分:2)
在Laravel-stapler中,还没有内置插值。但是您可以通过模型的构造函数传递产品ID来实现此目的。 试试这个..
在你的控制器中,
count
在你的模特中,
$model = new ProductGallery(array("product_id" => $product_id));
$model->fk_id = $product_id;
$model->image = Input::file('file');
$pathInfo = pathinfo(Input::file('file')->getClientOriginalName());
$newFilename = 'gallery-' . date("Ymd-") . sha1(time()) . '.' . $pathInfo['extension'];
$model->image->instanceWrite('file_name', $newFilename);
$model->save();
希望它对你有用。