Image handling

This commit is contained in:
Ruben 2016-09-12 17:27:06 +01:00
parent 9c692f7432
commit a6d7318aed
4 changed files with 75 additions and 8 deletions

View file

@ -95,16 +95,16 @@ class ScoreControllerProvider implements ControllerProviderInterface
// attributes // attributes
$hit->getAttributes()->setGlasses($data_hit['glasses']); $hit->getAttributes()->setGlasses($data_hit['glasses']);
foreach($hit->getAttributes()::$ATTRIBUTES as $attribute) { foreach($hit->getAttributes()->ATTRIBUTES as $attribute) {
$hit->getAttributes()->setAttribute($attribute, $data_hit[$attribute]); $hit->getAttributes()->setAttribute($attribute, $data_hit[$attribute]);
} }
// emotions // emotions
foreach($hit->getEmotions()::$EMOTIONS as $emotion) { foreach($hit->getEmotions()->EMOTIONS as $emotion) {
$hit->getEmotions()->setEmotion($emotion, $data_hit['emotions'][$emotion]); $hit->getEmotions()->setEmotion($emotion, $data_hit['emotions'][$emotion]);
} }
//expressions //expressions
foreach($hit->getExpressions()::$EXPRESSIONS as $expression) { foreach($hit->getExpressions()->EXPRESSIONS as $expression) {
$hit->getExpressions()->setExpression($expression, $data_hit['expressions'][$expression]); $hit->getExpressions()->setExpression($expression, $data_hit['expressions'][$expression]);
} }
@ -138,6 +138,59 @@ class ScoreControllerProvider implements ControllerProviderInterface
return new JsonResponse(['success' => true, 'id' => $game->getId(), 'hits' => $hits_array, 'achievements' => $achievement_ids], 200); return new JsonResponse(['success' => true, 'id' => $game->getId(), 'hits' => $hits_array, 'achievements' => $achievement_ids], 200);
}); });
/**
* Expects a full game + hits in the request
* add images as post multipart data.
* filename: hit_id-imagetype.jpg (ie. 12341-brows.jpg)
*/
$controllers->post('/images', function (Request $request, Application $app) {
$token = $app['security.token_storage']->getToken();
/* @var $user Models\User */
$user = $token->getUser();
if($request->files->count() < 1) {
return new JsonResponse(['success' => false, 'message' => "no files given"], 400);
}
$valid_parts = ['brows','mouth_left','mouth_right','nose'];
foreach($valid_parts as $part) {
$path = $app['file.path'] . "/hits/$part";
if(!is_dir($path)) {
mkdir($path, 0777, true);
}
}
foreach($request->files->all() as $file) {
/* @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */
$filename = $file->getClientOriginalName();
$ext = $file->getClientOriginalExtension();
if($ext != 'jpg' || $file->getMimeType() != 'image/jpeg') {
return new JsonResponse(['success' => false, 'message' => "Image not image/jpeg"], 400);
}
// name without .jpg
$id_string = substr($filename, 0, -4);
list($hit_id, $part) = \explode('-', $id_string);
if(!in_array($part, $valid_parts)) {
return new JsonResponse(['success' => false, 'message' => "unknown face part."], 400);
}
/* @var $hit Models\Hit */
$hit = $app['entity.manager']->getRepository(Models\Hit::class)->find($hit_id);
if(empty($hit) || $hit->getGame()->getUser()->getId() != $user->getId()) {
return new JsonResponse(['success' => false, 'message' => "Hit not found."], 400);
}
$file->move($app['file.path'] . "/hits/{$part}/", "{$hit->getId()}.jpg" );
}
return new JsonResponse(['success' => true, 'message' => "images saved."], 200);
});
return $controllers; return $controllers;
} }
} }

View file

@ -99,7 +99,7 @@ class Game
/** /**
* Gets the value of user. * Gets the value of user.
* *
* @return mixed * @return User
*/ */
public function getUser() public function getUser()
{ {
@ -252,7 +252,8 @@ class Game
if($diffExpression) { if($diffExpression) {
/* @var $diffExpressionText string */ /* @var $diffExpressionText string */
$diffExpressionText = $betterHit->getExpressions()::$EXPRESSIONS_2ND_PERSON[$diffExpression]; // $diffExpressionText = $betterHit->getExpressions()::$EXPRESSIONS_2ND_PERSON[$diffExpression];
$diffExpressionText = $betterHit->getExpressions()->get2ndPersonName($diffExpression);
$text .= " To show more empathy, try to " . $diffExpressionText ." " . $diff . "% " . $diffText ."."; $text .= " To show more empathy, try to " . $diffExpressionText ." " . $diff . "% " . $diffText .".";
} }

View file

@ -101,7 +101,7 @@ class Hit
/** /**
* Gets the value of game. * Gets the value of game.
* @return mixed * @return Game
*/ */
public function getGame() public function getGame()
{ {
@ -120,7 +120,7 @@ class Hit
/** /**
* Gets the value of attributes. * Gets the value of attributes.
* *
* @return mixed * @return Attributes
*/ */
public function getAttributes() public function getAttributes()
{ {
@ -130,7 +130,7 @@ class Hit
/** /**
* Gets the value of expressions. * Gets the value of expressions.
* *
* @return mixed * @return Expressions
*/ */
public function getExpressions() public function getExpressions()
{ {
@ -802,6 +802,17 @@ class Expressions {
} }
return $diffs; return $diffs;
} }
/**
* @param string $expression
* @return string
*/
public function get2ndPersonName(string $expression) {
if(!isSet(static::$EXPRESSIONS_2ND_PERSON[$expression])) {
return "";
}
return static::$EXPRESSIONS_2ND_PERSON[$expression];
}
} }
/** @ORM\Embeddable */ /** @ORM\Embeddable */

View file

@ -27,6 +27,8 @@ $app['entity.manager'] = function() {
return EmotionHero\Application::getInstance()->getEm(); return EmotionHero\Application::getInstance()->getEm();
}; };
$app['file.path'] = realpath(__DIR__ . "/../files");
$app['security.jwt'] = [ $app['security.jwt'] = [