SiteController.php в beckend/controllers
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index', 'save-redactor-img','save-img'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actionSaveRedactorImg($sub='main'){
$this->enableCsrfValidation = false;
if (Yii::$app->request->isPost) {
$dir = Yii::getAlias('@images').'/'.$sub.'/';
if(!file_exists($dir)){
FileHelper::createDirectory($dir);
}
$result_link = str_replace('admin.','',Url::home(true)).'uploads/images/'.$sub.'/';
$file = UploadedFile::getInstanceByName('file');
$model = new DynamicModel(compact('file'));
$model->addRule('file', 'image')->validate();
if ($model->hasErrors()) {
$result = [
'error' => $model->getFirstError('file')
];
} else {
$model->file->name = strtotime('now').'_'.Yii::$app->getSecurity()->generateRandomString(6) . '.' . $model->file->extension;
if ($model->file->saveAs($dir . $model->file->name)) {
$imag = Yii::$app->image->load($dir . $model->file->name);
$imag->resize(800, NULL, Yii\image\drivers\Image::PRECISE)->save($dir . $model->file->name, 85);
$result = ['filelink' => $result_link . $model->file->name, 'filename'=>$model->file->name];
} else {
$result = [
'error' => Yii::t('vova07/imperavi', 'ERROR_CAN_NOT_UPLOAD_FILE')
];
}
}
Yii::$app->response->format = Response::FORMAT_JSON;
return $result;
}else {
throw new BadRequestHttpException('Only POST is allowed');
}
}
ImageManager.php в common/models
class ImageManager extends \yii\db\ActiveRecord
{
public $attachment;
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'image_manager';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name', 'class', 'item_id'], 'required'],
[['item_id'], 'integer' ],
[['name', 'class', 'alt'], 'string', 'max' => 150],
[['attachment'], 'image'],
];
}
form.php в backend/views/blog
<?= $form->field($model, 'text')->widget(Widget::className(),[
'settings' => [
'lang' => 'ru',
'minHeight' => 200,
'formatting' => ['p', 'blockquote', 'h2', 'h1'],
'imageUpload' => \yii\helpers\Url::to(['/site/save-redactor-img','sub'=>'blog']),
'plugins' => [
'clips',
'fullscreen',
]
]
]) ?>
не могу понять дело в коде или в линуксе? не считая меня ;) подскажите пож. уважаемые IT-ки
-
Вопрос задан
-
53 просмотра