Example #1
0
 public function actionUpload()
 {
     return $this->goHome();
     $model = new UploadForm();
     if ($model->load(Yii::$app->request->post())) {
         // get the uploaded file instance. for multiple file uploads
         // the following data will return an array
         $image = UploadedFile::getInstance($model, 'image');
         // store the source file name
         $filename = $image->name;
         //    echo $filename; exit;
         //   $ext = end((explode(".", $image)));
         // generate a unique file name
         // $avatar = Yii::$app->security->generateRandomString().".{$ext}";
         // the path to save file, you can set an uploadPath
         // in Yii::$app->params (as used in example below)
         $path = Yii::$app->params['uploadPath'] . $filename;
         //    if($model->validate()){
         //    $image->saveAs($path);
         $image->saveAs('../uploads/' . $filename);
         return $this->redirect(['upload']);
         //  } else {
         // error in saving model
         //    }
     }
     return $this->render('upload', ['model' => $model]);
 }
 public function actionIndex()
 {
     // 'cos I can't install mcrypt on ma-webproxy-04 right now
     //Yii::$app->request->enableCsrfValidation = false;
     $model = new UploadForm();
     if (empty(Yii::$app->params['uploadPath'])) {
         Yii::$app->session->setFlash('error', 'Please configure the upload path');
     } elseif ($model->load(Yii::$app->request->post())) {
         return $this->loadImages($model);
     }
     return $this->render('index', ['model' => $model]);
 }
Example #3
0
 public function actionUploadcsv()
 {
     $model = new UploadForm();
     //        if (Yii::$app->request->isPost) {
     //            var_dump($_FILES);die;
     //        }
     if ($model->load(Yii::$app->request->post())) {
         $file = UploadedFile::getInstance($model, 'file');
         $model->file = $file->name;
         if ($model->validate()) {
             $file->saveAs(Yii::$app->basePath . '/uploads/' . $file->baseName . '.' . $file->extension);
             $model->processCsv($file);
         }
     }
     return $this->renderAjax('_upload_csv', ['model' => $model]);
 }
 public function actionUpload($key = null)
 {
     $readyPicsCount = Picture::find()->where(['state' => 'ready'])->count();
     $lastPictures = Picture::find()->where(['state' => 'ready'])->orderBy('updated ASC')->limit(50)->offset($readyPicsCount - 50)->all();
     $avgPictureTime = 0;
     if (count($lastPictures) > 1) {
         $summ = 0;
         for ($i = 1; $i < count($lastPictures); $i++) {
             $diff = strtotime($lastPictures[$i]->updated) - strtotime($lastPictures[$i - 1]->updated);
             $summ += $diff;
         }
         $avgPictureTime = intval($summ / (count($lastPictures) - 1));
     }
     $readyTime = $avgPictureTime * Picture::find()->where(['state' => 'new'])->count();
     $myPictureDP = new ActiveDataProvider(['query' => Picture::find()->where(['state' => 'new', 'ip' => Yii::$app->getRequest()->getUserIP()]), 'sort' => false]);
     $lastPending = Picture::find()->where(['state' => 'pending'])->orderBy('id DESC')->one();
     if ($lastPending == null) {
         $lastPending = Picture::find()->where(['state' => 'ready'])->orderBy('id DESC')->one();
     }
     $pendingPicsCount = Picture::find()->where(['state' => 'new'])->count();
     $algorithms = [];
     $algos = Algorithm::find()->orderBy('count DESC')->all();
     foreach ($algos as $algo) {
         if (count($algorithms) == 0) {
             $algorithms[$algo->getPrimaryKey()] = $algo->name . ' (default, ' . $algo->count . ' pics)';
         } else {
             $algorithms[$algo->getPrimaryKey()] = $algo->name . ' (' . $algo->count . ' pics)';
         }
     }
     $viewData = ['readyTime' => $readyTime, 'myPictureDP' => $myPictureDP, 'avgPictureTime' => $avgPictureTime, 'lastPendingId' => $lastPending->id, 'pendingPicsCount' => $pendingPicsCount, 'algorithms' => $algorithms, 'algos' => $algos];
     $priority = 0;
     if ($key != null) {
         $keyModel = Key::find()->where(['value' => $key])->andWhere('used < count')->one();
         if ($keyModel == null) {
             throw new HttpException(404, "Bad key");
         }
         $priority = $keyModel->priority;
         $viewData['key'] = $keyModel;
     }
     $model = new UploadForm();
     if ($model->load(Yii::$app->request->post())) {
         $image = UploadedFile::getInstance($model, 'image');
         $model->image = $image;
         if ($model->validate() && $model->check()) {
             $size = getimagesize($image->tempName);
             list($width, $height, $type) = $size;
             if ($type == IMAGETYPE_JPEG) {
                 $img = imagecreatefromjpeg($image->tempName);
             } else {
                 if ($type == IMAGETYPE_PNG) {
                     $img = imagecreatefrompng($image->tempName);
                 } else {
                     throw new HttpException(400, 'Bad image');
                 }
             }
             $srcName = Helper::gen_uuid() . '.jpg';
             $filename = \Yii::$app->basePath . '/web/images/' . $srcName;
             $k = 650;
             if (!($width <= $k && $height <= $k)) {
                 $minSide = (int) (min($width, $height) * $k / max($width, $height));
                 list($newWidth, $newHeight) = $width > $height ? [$k, $minSide] : [$minSide, $k];
                 $newImage = imagecreatetruecolor($newWidth, $newHeight);
                 imagecopyresampled($newImage, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
                 imagejpeg($newImage, $filename, 100);
             } else {
                 imagejpeg($img, $filename, 100);
             }
             $hash = sha1(file_get_contents($filename));
             $count = Picture::find()->where(['hash' => $hash, 'algorithmId' => $model->algoId])->all();
             if (count($count) > 0) {
                 unlink($filename);
                 $first = $count[0];
                 Yii::$app->getSession()->setFlash('success', 'This image was already submitted.');
                 return $this->redirect('/picture/' . $first->getPrimaryKey());
             }
             $algo = Algorithm::find()->where(['id' => $model->algoId])->one();
             $picture = new Picture();
             $picture->email = $model->email;
             $picture->ip = \Yii::$app->getRequest()->getUserIP();
             $picture->source = $srcName;
             $picture->output = null;
             $picture->state = 'new';
             $picture->hash = $hash;
             $picture->status = 0;
             $picture->priority = $priority;
             $picture->algorithm = $algo->name;
             $picture->algorithmId = $model->algoId;
             $picture->save();
             $algo->count += 1;
             $algo->save();
             if (!empty($keyModel)) {
                 $keyModel->used += 1;
                 $keyModel->save();
                 \Yii::$app->getSession()->setFlash('success', 'Your image were successfully uploaded. Converted image will be ready <b>ASAP</b> and sent on your email. Thank you!');
             } else {
                 \Yii::$app->getSession()->setFlash('success', 'Your image were successfully uploaded. Converted image will be ready after ~' . Helper::formatHourAndMin($readyTime) . ' and sent on your email. Thank you!');
             }
             return $this->redirect('/picture/' . $picture->getPrimaryKey());
         }
     }
     $viewData['model'] = $model;
     return $this->render('upload', $viewData);
 }
Example #5
0
 /**
  * Отвечает за загрузку изображений из формы редактирования записи
  * @return javascript string
  * @throws NotFoundHttpException if the model cannot be found
  */
 function actionUpload()
 {
     //return  ("<script>top.$('.mce-btn.mce-open').parent().find('.mce-textbox').val('/uploads/555efba5c24a7.jpg').closest('.mce-window').find('.mce-primary').click();</script>" );
     $upload_model = new UploadForm();
     $upload_model->load(yii::$app->request->post());
     $file = UploadedFile::getInstance($upload_model, 'file');
     if ($file->error === UPLOAD_ERR_OK) {
         try {
             $file_model = new Files();
             $file_model->saveImageFile($file);
             $file_model->post_id = $upload_model->post_id;
             $file_model->save();
             return "<script>top.\$('.mce-btn.mce-open').parent().find('.mce-textbox').val('/{$file_model->filename}').closest('.mce-window').find('.mce-primary').click();</script>";
         } catch (\Exception $e) {
         }
     }
     return "<script>\$.jGrowl('ошибка, файл не получен');</script>";
 }