public function actionIndex($algorithmId = null)
 {
     if ($algorithmId != null && Algorithm::find()->where(['id' => $algorithmId])->one() == null) {
         throw new HttpException(404, 'Haha but no');
     }
     $query = Picture::find()->where(['state' => 'ready']);
     if ($algorithmId != null) {
         $query = $query->andWhere(['algorithmId' => $algorithmId]);
     }
     $countQuery = clone $query;
     $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 50]);
     $pages->pageSizeParam = false;
     $pictures = $query->offset($pages->offset)->limit($pages->limit)->orderBy('likeCount DESC, id DESC')->all();
     $algorithms = Algorithm::find()->orderBy('count DESC')->all();
     return $this->render('index', ['pictures' => $pictures, 'paginator' => $pages, 'algorithms' => $algorithms, 'algorithmId' => $algorithmId]);
 }
 public function down()
 {
     \app\models\Algorithm::deleteAll();
     echo "Okay.\n";
 }
예제 #3
0
 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);
 }