示例#1
0
 public function updatePreview()
 {
     if (file_exists($this->realPath)) {
         $image = new SimpleImage();
         $image->load($this->realPath);
         $image->resizeToWidth(350);
         $image->save($this->realPreviewPath);
     }
 }
 public function actionCreate()
 {
     $files = UploadedFile::getInstancesByName('image');
     $className = Yii::$app->request->post('class');
     if (sizeof($files) > 0 && $className) {
         $ret = '';
         foreach ($files as $fileInstance) {
             if (array_search($fileInstance->type, Image::getAllowed()) === false) {
                 continue;
             }
             $fileName = md5(time() . $fileInstance->tempName) . "." . $fileInstance->extension;
             $path = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . Image::IMAGEFIELD_DIR . DIRECTORY_SEPARATOR . $fileName;
             $fileInstance->saveAs($path);
             $simpleImage = new SimpleImage();
             $simpleImage->load($path);
             if ($simpleImage->getWidth() > 1920 || $simpleImage->getHeight() > 1080) {
                 $simpleImage->resizeToWidth(1920);
                 $simpleImage->save($path);
             }
             $image = new Image();
             $image->file = $fileName;
             $image->class = $className;
             $tmp = explode('\\', $className);
             if (Yii::$app->request->post('field')) {
                 $image->field = Yii::$app->request->post('field');
             }
             if ($image->save()) {
                 if (Yii::$app->request->post('json')) {
                     return json_encode(['id' => $image->id, 'src' => $image->path]);
                 } else {
                     if (Yii::$app->request->post('field')) {
                         $ret .= Yii::$app->view->renderFile('@vendor/floor12/yii2-imagefield/views/_singleForm.php', ['field' => $image->field, 'image' => $image, 'class' => $tmp[2], 'hidden' => 0]);
                     } else {
                         $ret .= Yii::$app->view->renderFile('@vendor/floor12/yii2-imagefield/views/_form.php', ['image' => $image, 'class' => $className, 'hidden' => 1]);
                     }
                 }
             }
         }
         echo $ret;
     } else {
         throw new BadRequestHttpException();
     }
 }