1. Установил Yii2-images по этой ссылке https://github.com/CostaRico/yii2-images
1. Сделал миграции
Код: Выделить всё
php yii migrate/up --migrationPath=@vendor/costa-rico/yii2-images/migrations
2. В config->web.php добавил модуль
Код: Выделить всё
'yii2images' => [
'class' => 'rico\yii2images\Module',
//be sure, that permissions ok
//if you cant avoid permission errors you have to create "images" folder in web root manually and set 777 permissions
'imagesStorePath' => 'upload/store', //path to origin images
'imagesCachePath' => 'upload/cache', //path to resized copies
'graphicsLibrary' => 'GD', //but really its better to use 'Imagick'
'placeHolderPath' => '@webroot/images/placeHolder.png', // if you want to get placeholder when image not exists, string will be processed by Yii::getAlias
],
Код: Выделить всё
public function behaviors()
{
return [
'image' => [
'class' => 'rico\yii2images\behaviors\ImageBehave',
]
];
}
Код: Выделить всё
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->image = UploadedFile::getInstance($model,'image');
if(!empty($model->image)){
$model->upload();
}
// Yii::$app->session->setFlash('success',"Новость {$model->title_ua} обновлена");
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
Код: Выделить всё
public function upload(){//
if($this->validate()){
$path = 'upload/store/'.$this->image->baseName.'.'.$this->image->extension;
$this->image->saveAs($path);
$this->attachImage($path);
@unlink($path);
return true;
}else{
return false;
}
}
6. Теперь хочу вывести фото во view
7.Добавляю behaviors в модель.
Код: Выделить всё
public function behaviors()
{
return [
'image' => [
'class' => 'rico\yii2images\behaviors\ImageBehave',
]
];
}
Код: Выделить всё
public function actionNewsconsulate($id) {
$news = Consulate_news::findOne($id);
$main=$news->getImage();
if (is_null($news)) {
throw new NotFoundHttpException('The requested page does not exist.');
}
$sort = Consulate_news::find()
->where(['hide' => 0])
->orderBy(['date' => SORT_DESC])
->limit(8)
->all();
$this->setMeta($news->title_ua,$news->keywords_ua,$news->description_ua);
return $this->render(
'/site/news/consulate-news/news-consulate',
[
'news' => $news,
'main' => $main,
'sort' => $sort
]);
}
Код: Выделить всё
object(rico\yii2images\models\PlaceHolder)[101]
private 'modelName' => string '' (length=0)
private 'itemId' => string '' (length=0)
public 'filePath' => string 'placeHolder.png' (length=15)
public 'urlAlias' => string 'placeHolder' (length=11)
private 'helper' (rico\yii2images\models\Image) => boolean false
[b]private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=0)
empty[/b]
private '_oldAttributes' (yii\db\BaseActiveRecord) => null
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) => null
private '_module' (rico\yii2images\models\Image) =>
object(rico\yii2images\Module)[102]
public 'imagesStorePath' => string 'upload/store' (length=12)
public 'imagesCachePath' => string 'upload/cache' (length=12)
public 'graphicsLibrary' => string 'GD' (length=2)
public 'controllerNamespace' => string 'rico\yii2images\controllers' (length=27)
public 'placeHolderPath' => string '@webroot/images/placeHolder.png' (length=31)
public 'waterMark' => boolean false
public 'className' => null
public 'params' =>
array (size=0)
empty