public function actionUploadModal() { $message = false; $status = true; Yii::$app->response->format = Response::FORMAT_JSON; // Загружаем файл $file_upload = UploadedFile::getInstanceByName('images'); if (empty($file_upload)) { return []; } // Сохраняем файл $file_name = md5($file_upload->name . uniqid()); $file_ext = strrchr($file_upload->name, '.'); $file_dir = substr($file_name, 0, 2); $file_base_path = Yii::getAlias($this->module->base_path); FileHelper::createDirectory($file_base_path, 0777, true); $file_path = Yii::getAlias($this->module->base_path) . DIRECTORY_SEPARATOR . $file_dir; FileHelper::createDirectory($file_path, 0777, true); $file = $file_path . DIRECTORY_SEPARATOR . $file_name . $file_ext; //$imageTmp = Image::getImagine()->open($file_upload->tempName)->save($file); $imageTmp = Image::factory($file_upload->tempName, $this->module->image_driver); $imageTmp->save($file, 100); // Сохраняем в базу данных $model = new ImagesCommon(); $model->exp = $file_ext; $model->title = Yii::$app->request->post('name'); $model->name = $file_name; $model->path = $file_dir; $model->is_active = 1; if (!$model->save()) { Yii::error(['msg' => 'Ошибка сохрание загруженного изображения', 'data' => ['error' => $model->errors]]); unlink($file); $message = 'Ошибка загрузки изображения'; $status = false; } $url = $model->getImgOrigin(); $allow_size = $this->module->allow_size; if (is_array($allow_size)) { $btn_img = Html::beginTag('div', ['class' => 'col-md-12']); $btn_img .= 'Размеры: '; foreach ($allow_size as $size) { $btn_img .= Html::a($size, ['/mitrm_images/images-common/size', 'size' => $size, 'path' => $model->path, 'name' => $model->name], ['class' => 'btn btn-info js_mitrm_get_size_img', 'target' => '_blank']); } $btn_img .= Html::endTag('div'); } $preview = Html::img($url, ['style' => 'height: 100px;']); $url = 'http://' . $this->module->domain . $url; return ['status' => $status, 'url' => $url, 'message' => $message, 'preview' => $preview, 'img' => ['name' => $model->name, 'path' => $model->path], 'btn_img' => $btn_img]; }