Exemple #1
0
 public function actionUpload($model, $primaryKey)
 {
     $results = [];
     $modelImage = new Image();
     $modelImage->model = $model;
     $modelImage->primaryKey = $primaryKey;
     $filePath = $modelImage->getFilePath();
     if (Yii::$app->request->isPost) {
         $modelImage->file = UploadedFile::getInstance($modelImage, 'file');
         if ($modelImage->file && $modelImage->validate()) {
             $filename = $modelImage->file->name;
             $modelImage->src = $filename;
             $modelImage->position = $modelImage->nextPosition;
             $modelImage->save();
             $image = \Yii::$app->image->load($modelImage->file->tempName);
             $image->resize(2592, 1728, \yii\image\drivers\Image::AUTO);
             if ($image->save($filePath . '/' . $filename)) {
                 $imagePath = $filePath . '/' . $filename;
                 $result = ['name' => $filename, 'size' => filesize($filePath . '/' . $filename), 'url' => $imagePath, 'thumbnailUrl' => $modelImage->resize('100x100'), 'deleteUrl' => Url::to(['/core/image/delete', 'id' => $modelImage->id]), 'deleteType' => "DELETE"];
                 $results[] = $result;
             }
         } else {
             $results[] = (object) [$modelImage->file->name => false, 'error' => strip_tags(Html::error($modelImage, 'file')), 'extension' => $modelImage->file->extension, 'type' => $modelImage->file->type];
         }
     }
     echo json_encode((object) ['files' => $results]);
 }