diff --git a/controllers/ControlLogsController.php b/controllers/ControlLogsController.php index 1a8bebc3..1973583a 100644 --- a/controllers/ControlLogsController.php +++ b/controllers/ControlLogsController.php @@ -9,6 +9,8 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use app\models\ListManagement; +use yii\helpers\FileHelper; +use app\models\common; /** * CaptureLogsController implements the CRUD actions for CaptureLogs model. @@ -68,4 +70,68 @@ class ControlLogsController extends Controller { throw new NotFoundHttpException('The requested page does not exist.'); } + public function actionUseFeature($id) { + if (Yii::$app->request->isAjax) { + Yii::$app->response->format = "json"; + $model = $this->findModel($id); + $listManagement = ListManagement::findOne($model->staff_id); + if ($listManagement) { + $url = $model->image; + $images = json_decode($listManagement->image, true); + if (count($images) >= \Yii::$app->params['maxPicture']) + return ["status" => false, "text" => "Mỗi đối tượng chỉ nhận tối đa " . \Yii::$app->params['maxPicture'] . " hình ảnh mẫu"]; + $add = true; + foreach ($images as $key => $value) { + if ($value['urlOld'] === $url) + $add = false; + } + if ($add) { + $RootFolder = Yii::getAlias('@webroot') . "/data/uploads"; + $targetPath = $RootFolder . "/face"; + FileHelper::createDirectory($targetPath, 0777); + $fileName = "face_" . common::generateRandomString() . "_" . time() . ".png"; + $img = file_get_contents("http://localhost/data/uploads/face/" . $url); + $fileTarget = $targetPath . "/" . $fileName; + if (!$this->resizeImg($img, $fileTarget)) + $fileName = $url; + + $features = json_decode(common::requestToEngine("/get-feature", [ + "image_paths" => [ + ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] + ] + ]), true); + $images[] = [ + "url" => $fileName, + "urlOld" => $url, + "features" => $features['results'][0]['feature'], + "features512" => isset($features['results'][0]['feature512']) ? $features['results'][0]['feature512'] : [] + ]; + $listManagement->image = json_encode($images); + $listManagement->save(); + file_get_contents("http://localhost:2305/update-feature"); + return ["status" => true]; + } + return ["status" => false, "text" => "Hình ảnh này đã được chọn làm mẫu cho đối tượng này!"]; + } + return ["status" => false, "text" => "Đối tượng không tồn tại!"]; + } + } + + public function resizeImg($img, $fileTarget) { + $im = imagecreatefromstring($img); + $width = imagesx($im); + $height = imagesy($im); + $newwidth = 224; + $newheight = 224; + if ($width > $newwidth && $height > $newheight) { + $thumb = imagecreatetruecolor($newwidth, $newheight); + imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); + imagejpeg($thumb, $fileTarget); + imagedestroy($thumb); + imagedestroy($im); + return true; + } + return false; + } + } diff --git a/controllers/ListManagementController.php b/controllers/ListManagementController.php index db647734..f25ca372 100644 --- a/controllers/ListManagementController.php +++ b/controllers/ListManagementController.php @@ -585,7 +585,7 @@ class ListManagementController extends Controller { ]) ] ])), true); - return ["status" => true]; + return $res; //["status" => true]; } } diff --git a/helpers/CaptureLogsGrid.php b/helpers/CaptureLogsGrid.php index 9a03840f..f564b9f7 100644 --- a/helpers/CaptureLogsGrid.php +++ b/helpers/CaptureLogsGrid.php @@ -34,12 +34,24 @@ class CaptureLogsGrid { }; } - public static function image() { - return function($model) { - return Html::img("/data/uploads/face/" . $model->image, [ + public static function image($process = false) { + return function($model) use ($process) { + $btn = ""; + if ($process) + $btn = Html::button("", [ + "class" => "btn btn-info btn-xs", + "style" => "position:absolute;top:0;right:0;visibility: hidden;", + "data" => [ + "toggle" => "tooltip", + "href" => Url::to(["/control-logs/use-feature", "id" => $model->id]) + ], + "title" => "Chuyển làm ảnh mẫu", + "onclick" => "_useFeature(this);" + ]); + return "