Passed
Push — easter-egg ( b7db20 )
by Matias
08:02
created

FaceController::hipsterize()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 75
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 53
nc 7
nop 2
dl 0
loc 75
ccs 0
cts 57
cp 0
crap 30
rs 8.7143
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace OCA\FaceRecognition\Controller;
3
4
use OCP\Image as OCP_Image;
5
6
use OCP\IRequest;
7
use OCP\Files\IRootFolder;
8
use OCP\AppFramework\Http;
9
use OCP\AppFramework\Http\DataResponse;
10
use OCP\AppFramework\Http\JSONResponse;
11
use OCP\AppFramework\Http\DataDisplayResponse;
12
use OCP\AppFramework\Controller;
13
14
use OCA\FaceRecognition\Db\Face;
15
use OCA\FaceRecognition\Db\FaceMapper;
16
17
use OCA\FaceRecognition\Db\Image;
18
use OCA\FaceRecognition\Db\ImageMapper;
19
20
class FaceController extends Controller {
21
22
	private $rootFolder;
23
	private $faceMapper;
24
	private $imageMapper;
25
	private $userId;
26
27
	public function __construct($AppName,
28
	                            IRequest    $request,
29
	                            IRootFolder $rootFolder,
30
	                            FaceMapper  $faceMapper,
31
	                            ImageMapper $imageMapper,
32
	                            $UserId)
33
	{
34
		parent::__construct($AppName, $request);
35
		$this->rootFolder = $rootFolder;
36
		$this->faceMapper = $faceMapper;
37
		$this->imageMapper = $imageMapper;
38
		$this->userId = $UserId;
39
	}
40
41
	/**
42
	 * @NoAdminRequired
43
	 * @NoCSRFRequired
44
	 */
45
	public function getThumb ($id, $size) {
46
		$face = $this->faceMapper->find($id);
47
		$image = $this->imageMapper->find($this->userId, $face->getImage());
0 ignored issues
show
Bug introduced by
It seems like $face->getImage() can also be of type null; however, parameter $imageId of OCA\FaceRecognition\Db\ImageMapper::find() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
		$image = $this->imageMapper->find($this->userId, /** @scrutinizer ignore-type */ $face->getImage());
Loading history...
48
		$fileId = $image->getFile();
49
50
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
51
		$nodes = $userFolder->getById($fileId);
52
		$file = $nodes[0];
53
54
		$ownerView = new \OC\Files\View('/'. $this->userId . '/files');
0 ignored issues
show
Bug introduced by
The type OC\Files\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
		$path = $userFolder->getRelativePath($file->getPath());
56
57
		$img = new OCP_Image();
58
		$fileName = $ownerView->getLocalFile($path);
59
		$img->loadFromFile($fileName);
60
		$img->fixOrientation();
61
62
		$x = $face->getLeft ();
63
		$y = $face->getTop ();
64
		$w = $face->getRight () - $x;
65
		$h = $face->getBottom () - $y;
66
67
		$padding = $h*0.25;
68
		$x -= $padding;
69
		$y -= $padding;
70
		$w += $padding*2;
71
		$h += $padding*2;
72
73
		if (true) {
74
			$this->hipsterize($img, $face);
75
		}
76
77
		$img->crop($x, $y, $w, $h);
78
		$img->scaleDownToFit($size, $size);
79
80
		$resp = new DataDisplayResponse($img->data(), Http::STATUS_OK, ['Content-Type' => $img->mimeType()]);
81
		$resp->setETag((string)crc32($img->data()));
82
		$resp->cacheFor(7 * 24 * 60 * 60);
83
		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
84
85
		return $resp;
86
	}
87
88
	/** @scrutinizer ignore-unused */
89
	private function hipsterize(&$image, &$face) {
90
		$imgResource = $image->resource();
91
92
		$landmarks = json_decode($face->getLandmarks(), true);
93
		if (count($landmarks) === 5) {
94
			$eyesX1 = $landmarks[2]['x'];
95
			$eyesY1 = $landmarks[2]['y'];
96
97
			$eyesX2 = $landmarks[0]['x'];
98
			$eyesY2 = $landmarks[0]['y'];
99
100
			$eyesXC = ($eyesX2 + $eyesX1)/2;
101
			$eyesYC = ($eyesY2 + $eyesY1)/2;
102
103
			$mustacheXC = $landmarks[4]['x'];
104
			$mustacheYC = $landmarks[4]['y'];
105
		}
106
		else if (count($landmarks) === 68) {
107
			$eyesX1 = $landmarks[36]['x'];
108
			$eyesY1 = $landmarks[36]['y'];
109
			$eyesX2 = $landmarks[45]['x'];
110
			$eyesY2 = $landmarks[45]['y'];
111
112
			$eyesXC = ($eyesX2 + $eyesX1)/2;
113
			$eyesYC = ($eyesY2 + $eyesY1)/2;
114
115
			$mustacheXC = $landmarks[52]['x'];
116
			$mustacheYC = $landmarks[52]['y'];
117
		}
118
		else {
119
			return;
120
		}
121
122
		$eyesW = $eyesX2 - $eyesX1;
123
		$eyesH = $eyesY2 - $eyesY1;
124
125
		$eyesL = sqrt(pow($eyesW, 2) + pow($eyesH, 2));
126
		$angle = rad2deg(atan(-$eyesH/$eyesW));
127
128
		$glassesGd = imagecreatefrompng(\OC_App::getAppPath('facerecognition') . '/img/glasses.png');
0 ignored issues
show
Bug introduced by
The type OC_App was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
129
		if ($glassesGd === false)
130
			return;
131
		$fillColor = imagecolorallocatealpha($glassesGd, 0, 0, 0, 127);
132
		$glassesGd = imagerotate($glassesGd, $angle, $fillColor);
133
134
		$glassesW = imagesx($glassesGd);
0 ignored issues
show
Bug introduced by
It seems like $glassesGd can also be of type false; however, parameter $image of imagesx() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
		$glassesW = imagesx(/** @scrutinizer ignore-type */ $glassesGd);
Loading history...
135
		$glassesH = imagesy($glassesGd);
0 ignored issues
show
Bug introduced by
It seems like $glassesGd can also be of type false; however, parameter $image of imagesy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
		$glassesH = imagesy(/** @scrutinizer ignore-type */ $glassesGd);
Loading history...
136
137
		$glassesRatio = $eyesL/$glassesW*1.5;
138
139
		$glassesDestX = intval($eyesXC - $glassesW * $glassesRatio / 2);
140
		$glassesDestY = intval($eyesYC - $glassesH * $glassesRatio / 2);
141
		$glassesDestW = intval($glassesW * $glassesRatio);
142
		$glassesDestH = intval($glassesH * $glassesRatio);
143
144
		imagecopyresized($imgResource, $glassesGd, $glassesDestX, $glassesDestY, 0, 0, $glassesDestW, $glassesDestH, $glassesW, $glassesH);
0 ignored issues
show
Bug introduced by
It seems like $glassesGd can also be of type false; however, parameter $src_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
		imagecopyresized($imgResource, /** @scrutinizer ignore-type */ $glassesGd, $glassesDestX, $glassesDestY, 0, 0, $glassesDestW, $glassesDestH, $glassesW, $glassesH);
Loading history...
145
146
		$mustacheGd = imagecreatefrompng(\OC_App::getAppPath('facerecognition') . '/img/mustache.png');
147
		if ($mustacheGd === false)
148
			return;
149
		$fillColor = imagecolorallocatealpha($mustacheGd, 0, 0, 0, 127);
150
		$mustacheGd = imagerotate($mustacheGd, $angle, $fillColor);
151
		$mustacheW = imagesx($mustacheGd);
152
		$mustacheH = imagesy($mustacheGd);
153
154
		$mustacheRatio = $eyesL/$glassesW*1.1;
155
156
		$mustacheDestX = intval($mustacheXC - $mustacheW * $mustacheRatio / 2);
157
		$mustacheDestY = intval($mustacheYC - $mustacheH * $mustacheRatio / 2);
158
		$mustacheDestW = intval($mustacheW * $mustacheRatio);
159
		$mustacheDestH = intval($mustacheH * $mustacheRatio);
160
161
		imagecopyresized($imgResource, $mustacheGd, $mustacheDestX, $mustacheDestY, 0, 0, $mustacheDestW, $mustacheDestH, $mustacheW, $mustacheH);
162
163
		$image->setResource($imgResource);
164
	}
165
166
}
167