Модель
Код: Выделить всё
public $imageFile;
public function rules()
{
return [
...
['imageFile', 'required', 'on' => static::SCENARIO_CREATE],
['imageFile', 'file', 'mimeTypes' => ['image/png', 'image/jpeg', 'image/gif']],
];
}
public function upload()
{
Image::thumbnail($this->imageFile->tempName, 27, 21, ManipulatorInterface::THUMBNAIL_INSET)
->save(Yii::getAlias('@webroot/uploads/i10n/') . $this->imageFile->baseName . '.' . $this->imageFile->extension);
return true;
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if ($this->imageFile !== null) {
return $this->upload();
}
}
return true;
}
public function beforeValidate()
{
if (parent::beforeValidate()) {
$this->imageFile = UploadedFile::getInstance($this, 'imageFile');
}
return true;
}
В контроллере всё стандартно.