我正在尝试使用我在 Web 应用程序上使用的模型的Uploader
插件。我已经将它与我在这里展示的配置一起使用:Behavior
User
filename
<?php
class Avatar extends AppModel {
public $name = 'Avatar';
public $actsAs = array (
'Uploader.Attachment' => array (
'filename' => array (
'name' => 'setNameAsImgId',
'saveAsFilename' => true,
'uploadDir' => '/files/avatars/160/',
'dbColumn' => 'filename',
'defaultPath' => 'default.png',
'maxNameLength' => 20,
'overwrite' => true,
'stopSave' => true,
'allowEmpty' => false,
'transforms' => array (
array('method' => 'resize', 'width' => 160, 'height' => 160, 'dbColumn' => 'filename', 'append' => false, 'overwrite' => true)
)
)
)
);
// and so on...
?>
此配置将文件保存到我通过控制器/files/avatars/160
使用此方法保存所有大小为 160 像素的图像的文件夹中:Users
<?php
class UsersController extends AppController {
public function add () {
if ($this->User->Avatar->save($this->request->data)) {
// do some code...
}
}
在视图中add
,我将插入此代码以上传文件:
echo $this->Form->input('Avatar.filename', array('type' => 'file', 'label'=>'Upload the avatar '));
如果我只保存filename
上例中命名的一个配置,这很好用,我的问题是我需要保存同一图像的不同尺寸和裁剪,所以我想保存一个尺寸列表,如下所示:
/files/avatars/160/filename.jpg
/files/avatars/48/filename.jpg
为此,我想向我的Avatar
模型添加另一个配置
<?php
class Avatar extends AppModel {
public $name = 'Avatar';
public $actsAs = array (
'Uploader.Attachment' => array (
'filename' => array (
'name' => 'setNameAsImgId',
'saveAsFilename' => true,
'uploadDir' => '/files/avatars/160/',
'dbColumn' => 'filename',
'defaultPath' => 'default.png',
'maxNameLength' => 20,
'overwrite' => true,
'stopSave' => true,
'allowEmpty' => false,
'transforms' => array (
array('method' => 'resize', 'width' => 160, 'height' => 160, 'dbColumn' => 'filename', 'append' => false, 'overwrite' => true)
)
),
'small' => array (
'name' => 'setNameAsImgId',
'saveAsFilename' => true,
'uploadDir' => '/files/avatars/48/',
'dbColumn' => 'filename',
'defaultPath' => 'default.png',
'maxNameLength' => 20,
'overwrite' => true,
'stopSave' => true,
'allowEmpty' => false,
'transforms' => array (
array('method' => 'resize', 'width' => 48, 'height' => 48, 'dbColumn' => 'filename', 'append' => false, 'overwrite' => true)
)
)
)
);
// and so on...
?>
我应该如何设置UsersController
以保存同一图像的多个裁剪Avatar
?我是否还应该更改add
视图以使其正常工作?我不会在表单中使用多个输入文件。