update button add feature - control logs site
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user