From a1978f5a16cf13cafcb05c2a157b2eab97eaafc0 Mon Sep 17 00:00:00 2001 From: Ruben Date: Thu, 3 Nov 2016 00:14:19 +0100 Subject: [PATCH] add GET /interface/images --- src/Api/InterfaceControllerProvider.php | 64 +++++++++++++++++++++++++ www/index.php | 1 + 2 files changed, 65 insertions(+) create mode 100644 src/Api/InterfaceControllerProvider.php diff --git a/src/Api/InterfaceControllerProvider.php b/src/Api/InterfaceControllerProvider.php new file mode 100644 index 0000000..71480a3 --- /dev/null +++ b/src/Api/InterfaceControllerProvider.php @@ -0,0 +1,64 @@ +_eh = EH::getInstance(); + } + + public function connect(Application $app) + { + // creates a new controller based on the default route + $controllers = $app['controllers_factory']; + + $controllers->get('/images', function (Application $app) { + + $getEmoBlocks = function($emotion, $feature, $steps = 6) use($em) { + $hitRepo = $this->_em->getEm()->getRepository(EmotionHero\Models\Hit::class); + $blocks = []; + foreach(range(0, 100, 100/($steps-1)) as $i) { + /* @var $hitRepo EmotionHero\Models\HitRepository */ + $hit = $hitRepo->getClosestHitWithImage($emotion, $i); + $img = "data:image/x-icon;base64,".$hit->getFeatureImgAsString($feature); + $score = $hit->getEmotions()->getEmotionScore($emotion); + $percentage = sprintf("%.0f %%",$score); + $blocks[] = [ + 'hit_id' => $hit->getId(), + 'img_data' => $img, + 'percentage' => $percentage, + ]; + } + return $blocks; + }; + + $features = ['brows' => 6, 'nose' => 5, 'mouth' => 8]; + + $output = []; + + foreach($features as $feature => $steps) { + foreach(Models\Emotions::$EMOTIONS as $emotion) { + $output[$feature][$emotion] = $getEmoBlocks( + $emotion, + $feature == 'mouth' ? 'mouth_left':$feature, + $steps + ); + } + } + + return $app['serializer']->serialize($output, 'json'); + }); + } +} diff --git a/www/index.php b/www/index.php index 2c24e17..3eb7791 100644 --- a/www/index.php +++ b/www/index.php @@ -165,6 +165,7 @@ $app->get('/api/protected_resource', function() use ($app){ $app->mount('/', new EmotionHero\Api\ScoreControllerProvider()); +$app->mount('/interface', new EmotionHero\Api\InterfaceControllerProvider()); // middlewares $appStack = new EmotionHero\Api\ThrottleMiddleware($app, ['pdo'=>$eh->getEm()->getConnection()->getWrappedConnection()] );