@@ -295,6 +295,7 @@ discard block |
||
295 | 295 | * Calculate steps between two Colors |
296 | 296 | * @param object Color $steps start color |
297 | 297 | * @param object Color $ends end color |
298 | + * @param integer $steps |
|
298 | 299 | * @return array [r,g,b] steps for each color to go from $steps to $ends |
299 | 300 | */ |
300 | 301 | private function stepCalc($steps, $ends) { |
@@ -306,8 +307,9 @@ discard block |
||
306 | 307 | } |
307 | 308 | /** |
308 | 309 | * Convert a string to an integer evenly |
309 | - * @param string $hash the text to parse |
|
310 | - * @param int $maximum the maximum range |
|
310 | + * @param integer $steps |
|
311 | + * @param Color $color1 |
|
312 | + * @param Color $color2 |
|
311 | 313 | * @return int between 0 and $maximum |
312 | 314 | */ |
313 | 315 | private function mixPalette($steps, $color1, $color2) { |
@@ -47,345 +47,345 @@ |
||
47 | 47 | */ |
48 | 48 | |
49 | 49 | class Avatar implements IAvatar { |
50 | - /** @var ISimpleFolder */ |
|
51 | - private $folder; |
|
52 | - /** @var IL10N */ |
|
53 | - private $l; |
|
54 | - /** @var User */ |
|
55 | - private $user; |
|
56 | - /** @var ILogger */ |
|
57 | - private $logger; |
|
58 | - /** @var IConfig */ |
|
59 | - private $config; |
|
60 | - |
|
61 | - /** |
|
62 | - * constructor |
|
63 | - * |
|
64 | - * @param ISimpleFolder $folder The folder where the avatars are |
|
65 | - * @param IL10N $l |
|
66 | - * @param User $user |
|
67 | - * @param ILogger $logger |
|
68 | - * @param IConfig $config |
|
69 | - */ |
|
70 | - public function __construct(ISimpleFolder $folder, |
|
71 | - IL10N $l, |
|
72 | - $user, |
|
73 | - ILogger $logger, |
|
74 | - IConfig $config) { |
|
75 | - $this->folder = $folder; |
|
76 | - $this->l = $l; |
|
77 | - $this->user = $user; |
|
78 | - $this->logger = $logger; |
|
79 | - $this->config = $config; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @inheritdoc |
|
84 | - */ |
|
85 | - public function get ($size = 64) { |
|
86 | - try { |
|
87 | - $file = $this->getFile($size); |
|
88 | - } catch (NotFoundException $e) { |
|
89 | - return false; |
|
90 | - } |
|
91 | - |
|
92 | - $avatar = new OC_Image(); |
|
93 | - $avatar->loadFromData($file->getContent()); |
|
94 | - return $avatar; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Check if an avatar exists for the user |
|
99 | - * |
|
100 | - * @return bool |
|
101 | - */ |
|
102 | - public function exists() { |
|
103 | - |
|
104 | - return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * sets the users avatar |
|
109 | - * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
110 | - * @throws \Exception if the provided file is not a jpg or png image |
|
111 | - * @throws \Exception if the provided image is not valid |
|
112 | - * @throws NotSquareException if the image is not square |
|
113 | - * @return void |
|
114 | - */ |
|
115 | - public function set ($data) { |
|
116 | - |
|
117 | - if($data instanceOf IImage) { |
|
118 | - $img = $data; |
|
119 | - $data = $img->data(); |
|
120 | - } else { |
|
121 | - $img = new OC_Image(); |
|
122 | - if (is_resource($data) && get_resource_type($data) === "gd") { |
|
123 | - $img->setResource($data); |
|
124 | - } elseif(is_resource($data)) { |
|
125 | - $img->loadFromFileHandle($data); |
|
126 | - } else { |
|
127 | - try { |
|
128 | - // detect if it is a path or maybe the images as string |
|
129 | - $result = @realpath($data); |
|
130 | - if ($result === false || $result === null) { |
|
131 | - $img->loadFromData($data); |
|
132 | - } else { |
|
133 | - $img->loadFromFile($data); |
|
134 | - } |
|
135 | - } catch (\Error $e) { |
|
136 | - $img->loadFromData($data); |
|
137 | - } |
|
138 | - } |
|
139 | - } |
|
140 | - $type = substr($img->mimeType(), -3); |
|
141 | - if ($type === 'peg') { |
|
142 | - $type = 'jpg'; |
|
143 | - } |
|
144 | - if ($type !== 'jpg' && $type !== 'png') { |
|
145 | - throw new \Exception($this->l->t('Unknown filetype')); |
|
146 | - } |
|
147 | - |
|
148 | - if (!$img->valid()) { |
|
149 | - throw new \Exception($this->l->t('Invalid image')); |
|
150 | - } |
|
151 | - |
|
152 | - if (!($img->height() === $img->width())) { |
|
153 | - throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
154 | - } |
|
155 | - |
|
156 | - $this->remove(); |
|
157 | - $file = $this->folder->newFile('avatar.'.$type); |
|
158 | - $file->putContent($data); |
|
159 | - |
|
160 | - try { |
|
161 | - $generated = $this->folder->getFile('generated'); |
|
162 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
163 | - $generated->delete(); |
|
164 | - } catch (NotFoundException $e) { |
|
165 | - // |
|
166 | - } |
|
167 | - $this->user->triggerChange('avatar', $file); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * remove the users avatar |
|
172 | - * @return void |
|
173 | - */ |
|
174 | - public function remove () { |
|
175 | - $avatars = $this->folder->getDirectoryListing(); |
|
176 | - |
|
177 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
178 | - (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
179 | - |
|
180 | - foreach ($avatars as $avatar) { |
|
181 | - $avatar->delete(); |
|
182 | - } |
|
183 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
184 | - $this->user->triggerChange('avatar', ''); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * @inheritdoc |
|
189 | - */ |
|
190 | - public function getFile($size) { |
|
191 | - try { |
|
192 | - $ext = $this->getExtension(); |
|
193 | - } catch (NotFoundException $e) { |
|
194 | - $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
195 | - $avatar = $this->folder->newFile('avatar.png'); |
|
196 | - $avatar->putContent($data); |
|
197 | - $ext = 'png'; |
|
198 | - |
|
199 | - $this->folder->newFile('generated'); |
|
200 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
201 | - } |
|
202 | - |
|
203 | - if ($size === -1) { |
|
204 | - $path = 'avatar.' . $ext; |
|
205 | - } else { |
|
206 | - $path = 'avatar.' . $size . '.' . $ext; |
|
207 | - } |
|
208 | - |
|
209 | - try { |
|
210 | - $file = $this->folder->getFile($path); |
|
211 | - } catch (NotFoundException $e) { |
|
212 | - if ($size <= 0) { |
|
213 | - throw new NotFoundException; |
|
214 | - } |
|
215 | - |
|
216 | - if ($this->folder->fileExists('generated')) { |
|
217 | - $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
218 | - |
|
219 | - } else { |
|
220 | - $avatar = new OC_Image(); |
|
221 | - /** @var ISimpleFile $file */ |
|
222 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
223 | - $avatar->loadFromData($file->getContent()); |
|
224 | - $avatar->resize($size); |
|
225 | - $data = $avatar->data(); |
|
226 | - } |
|
227 | - |
|
228 | - try { |
|
229 | - $file = $this->folder->newFile($path); |
|
230 | - $file->putContent($data); |
|
231 | - } catch (NotPermittedException $e) { |
|
232 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
233 | - throw new NotFoundException(); |
|
234 | - } |
|
235 | - |
|
236 | - } |
|
237 | - |
|
238 | - if($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
239 | - $generated = $this->folder->fileExists('generated') ? 'true' : 'false'; |
|
240 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', $generated); |
|
241 | - } |
|
242 | - |
|
243 | - return $file; |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Get the extension of the avatar. If there is no avatar throw Exception |
|
248 | - * |
|
249 | - * @return string |
|
250 | - * @throws NotFoundException |
|
251 | - */ |
|
252 | - private function getExtension() { |
|
253 | - if ($this->folder->fileExists('avatar.jpg')) { |
|
254 | - return 'jpg'; |
|
255 | - } elseif ($this->folder->fileExists('avatar.png')) { |
|
256 | - return 'png'; |
|
257 | - } |
|
258 | - throw new NotFoundException; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * @param string $userDisplayName |
|
263 | - * @param int $size |
|
264 | - * @return string |
|
265 | - */ |
|
266 | - private function generateAvatar($userDisplayName, $size) { |
|
267 | - $text = mb_strtoupper(mb_substr($userDisplayName, 0, 1), 'UTF-8'); |
|
268 | - $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
269 | - |
|
270 | - $im = imagecreatetruecolor($size, $size); |
|
271 | - $background = imagecolorallocate($im, $backgroundColor->r, $backgroundColor->g, $backgroundColor->b); |
|
272 | - $white = imagecolorallocate($im, 255, 255, 255); |
|
273 | - imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
274 | - |
|
275 | - $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.ttf'; |
|
276 | - |
|
277 | - $fontSize = $size * 0.4; |
|
278 | - $box = imagettfbbox($fontSize, 0, $font, $text); |
|
279 | - |
|
280 | - $x = ($size - ($box[2] - $box[0])) / 2; |
|
281 | - $y = ($size - ($box[1] - $box[7])) / 2; |
|
282 | - $x += 1; |
|
283 | - $y -= $box[7]; |
|
284 | - imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
285 | - |
|
286 | - ob_start(); |
|
287 | - imagepng($im); |
|
288 | - $data = ob_get_contents(); |
|
289 | - ob_end_clean(); |
|
290 | - |
|
291 | - return $data; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Calculate steps between two Colors |
|
296 | - * @param object Color $steps start color |
|
297 | - * @param object Color $ends end color |
|
298 | - * @return array [r,g,b] steps for each color to go from $steps to $ends |
|
299 | - */ |
|
300 | - private function stepCalc($steps, $ends) { |
|
301 | - $step = array(); |
|
302 | - $step[0] = ($ends[1]->r - $ends[0]->r) / $steps; |
|
303 | - $step[1] = ($ends[1]->g - $ends[0]->g) / $steps; |
|
304 | - $step[2] = ($ends[1]->b - $ends[0]->b) / $steps; |
|
305 | - return $step; |
|
306 | - } |
|
307 | - /** |
|
308 | - * Convert a string to an integer evenly |
|
309 | - * @param string $hash the text to parse |
|
310 | - * @param int $maximum the maximum range |
|
311 | - * @return int between 0 and $maximum |
|
312 | - */ |
|
313 | - private function mixPalette($steps, $color1, $color2) { |
|
314 | - $count = $steps + 1; |
|
315 | - $palette = array($color1); |
|
316 | - $step = $this->stepCalc($steps, [$color1, $color2]); |
|
317 | - for ($i = 1; $i < $steps; $i++) { |
|
318 | - $r = intval($color1->r + ($step[0] * $i)); |
|
319 | - $g = intval($color1->g + ($step[1] * $i)); |
|
320 | - $b = intval($color1->b + ($step[2] * $i)); |
|
321 | - $palette[] = new Color($r, $g, $b); |
|
322 | - } |
|
323 | - return $palette; |
|
324 | - } |
|
325 | - |
|
326 | - |
|
327 | - /** |
|
328 | - * Convert a string to an integer evenly |
|
329 | - * @param string $hash the text to parse |
|
330 | - * @param int $maximum the maximum range |
|
331 | - * @return int between 0 and $maximum |
|
332 | - */ |
|
333 | - private function hashToInt($hash, $maximum) { |
|
334 | - $final = 0; |
|
335 | - $result = array(); |
|
336 | - |
|
337 | - // Splitting evenly the string |
|
338 | - for ($i=0; $i< strlen($hash); $i++) { |
|
339 | - // chars in md5 goes up to f, hex:16 |
|
340 | - $result[] = intval(substr($hash, $i, 1), 16) % 16; |
|
341 | - } |
|
342 | - // Adds up all results |
|
343 | - foreach ($result as $value) { |
|
344 | - $final += $value; |
|
345 | - } |
|
346 | - // chars in md5 goes up to f, hex:16 |
|
347 | - return intval($final % $maximum); |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * @param string $text |
|
353 | - * @return Color Object containting r g b int in the range [0, 255] |
|
354 | - */ |
|
355 | - function avatarBackgroundColor($text) { |
|
356 | - $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
357 | - |
|
358 | - $hash = md5($hash); |
|
359 | - $hashChars = str_split($hash); |
|
360 | - |
|
361 | - $red = new Color(182, 70, 157); |
|
362 | - $yellow = new Color(221, 203, 85); |
|
363 | - $blue = new Color(0, 130, 201); // Nextcloud blue |
|
364 | - // Number of steps to go from a color to another |
|
365 | - // 3 colors * 6 will result in 18 generated colors |
|
366 | - $steps = 6; |
|
367 | - |
|
368 | - $palette1 = $this->mixPalette($steps, $red, $yellow); |
|
369 | - $palette2 = $this->mixPalette($steps, $yellow, $blue); |
|
370 | - $palette3 = $this->mixPalette($steps, $blue, $red); |
|
371 | - |
|
372 | - $finalPalette = array_merge($palette1, $palette2, $palette3); |
|
373 | - |
|
374 | - return $finalPalette[$this->hashToInt($hash, $steps * 3 )]; |
|
375 | - } |
|
376 | - |
|
377 | - public function userChanged($feature, $oldValue, $newValue) { |
|
378 | - // We only change the avatar on display name changes |
|
379 | - if ($feature !== 'displayName') { |
|
380 | - return; |
|
381 | - } |
|
382 | - |
|
383 | - // If the avatar is not generated (so an uploaded image) we skip this |
|
384 | - if (!$this->folder->fileExists('generated')) { |
|
385 | - return; |
|
386 | - } |
|
387 | - |
|
388 | - $this->remove(); |
|
389 | - } |
|
50 | + /** @var ISimpleFolder */ |
|
51 | + private $folder; |
|
52 | + /** @var IL10N */ |
|
53 | + private $l; |
|
54 | + /** @var User */ |
|
55 | + private $user; |
|
56 | + /** @var ILogger */ |
|
57 | + private $logger; |
|
58 | + /** @var IConfig */ |
|
59 | + private $config; |
|
60 | + |
|
61 | + /** |
|
62 | + * constructor |
|
63 | + * |
|
64 | + * @param ISimpleFolder $folder The folder where the avatars are |
|
65 | + * @param IL10N $l |
|
66 | + * @param User $user |
|
67 | + * @param ILogger $logger |
|
68 | + * @param IConfig $config |
|
69 | + */ |
|
70 | + public function __construct(ISimpleFolder $folder, |
|
71 | + IL10N $l, |
|
72 | + $user, |
|
73 | + ILogger $logger, |
|
74 | + IConfig $config) { |
|
75 | + $this->folder = $folder; |
|
76 | + $this->l = $l; |
|
77 | + $this->user = $user; |
|
78 | + $this->logger = $logger; |
|
79 | + $this->config = $config; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @inheritdoc |
|
84 | + */ |
|
85 | + public function get ($size = 64) { |
|
86 | + try { |
|
87 | + $file = $this->getFile($size); |
|
88 | + } catch (NotFoundException $e) { |
|
89 | + return false; |
|
90 | + } |
|
91 | + |
|
92 | + $avatar = new OC_Image(); |
|
93 | + $avatar->loadFromData($file->getContent()); |
|
94 | + return $avatar; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Check if an avatar exists for the user |
|
99 | + * |
|
100 | + * @return bool |
|
101 | + */ |
|
102 | + public function exists() { |
|
103 | + |
|
104 | + return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * sets the users avatar |
|
109 | + * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
110 | + * @throws \Exception if the provided file is not a jpg or png image |
|
111 | + * @throws \Exception if the provided image is not valid |
|
112 | + * @throws NotSquareException if the image is not square |
|
113 | + * @return void |
|
114 | + */ |
|
115 | + public function set ($data) { |
|
116 | + |
|
117 | + if($data instanceOf IImage) { |
|
118 | + $img = $data; |
|
119 | + $data = $img->data(); |
|
120 | + } else { |
|
121 | + $img = new OC_Image(); |
|
122 | + if (is_resource($data) && get_resource_type($data) === "gd") { |
|
123 | + $img->setResource($data); |
|
124 | + } elseif(is_resource($data)) { |
|
125 | + $img->loadFromFileHandle($data); |
|
126 | + } else { |
|
127 | + try { |
|
128 | + // detect if it is a path or maybe the images as string |
|
129 | + $result = @realpath($data); |
|
130 | + if ($result === false || $result === null) { |
|
131 | + $img->loadFromData($data); |
|
132 | + } else { |
|
133 | + $img->loadFromFile($data); |
|
134 | + } |
|
135 | + } catch (\Error $e) { |
|
136 | + $img->loadFromData($data); |
|
137 | + } |
|
138 | + } |
|
139 | + } |
|
140 | + $type = substr($img->mimeType(), -3); |
|
141 | + if ($type === 'peg') { |
|
142 | + $type = 'jpg'; |
|
143 | + } |
|
144 | + if ($type !== 'jpg' && $type !== 'png') { |
|
145 | + throw new \Exception($this->l->t('Unknown filetype')); |
|
146 | + } |
|
147 | + |
|
148 | + if (!$img->valid()) { |
|
149 | + throw new \Exception($this->l->t('Invalid image')); |
|
150 | + } |
|
151 | + |
|
152 | + if (!($img->height() === $img->width())) { |
|
153 | + throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
154 | + } |
|
155 | + |
|
156 | + $this->remove(); |
|
157 | + $file = $this->folder->newFile('avatar.'.$type); |
|
158 | + $file->putContent($data); |
|
159 | + |
|
160 | + try { |
|
161 | + $generated = $this->folder->getFile('generated'); |
|
162 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
163 | + $generated->delete(); |
|
164 | + } catch (NotFoundException $e) { |
|
165 | + // |
|
166 | + } |
|
167 | + $this->user->triggerChange('avatar', $file); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * remove the users avatar |
|
172 | + * @return void |
|
173 | + */ |
|
174 | + public function remove () { |
|
175 | + $avatars = $this->folder->getDirectoryListing(); |
|
176 | + |
|
177 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
178 | + (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
179 | + |
|
180 | + foreach ($avatars as $avatar) { |
|
181 | + $avatar->delete(); |
|
182 | + } |
|
183 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
184 | + $this->user->triggerChange('avatar', ''); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * @inheritdoc |
|
189 | + */ |
|
190 | + public function getFile($size) { |
|
191 | + try { |
|
192 | + $ext = $this->getExtension(); |
|
193 | + } catch (NotFoundException $e) { |
|
194 | + $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
195 | + $avatar = $this->folder->newFile('avatar.png'); |
|
196 | + $avatar->putContent($data); |
|
197 | + $ext = 'png'; |
|
198 | + |
|
199 | + $this->folder->newFile('generated'); |
|
200 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
201 | + } |
|
202 | + |
|
203 | + if ($size === -1) { |
|
204 | + $path = 'avatar.' . $ext; |
|
205 | + } else { |
|
206 | + $path = 'avatar.' . $size . '.' . $ext; |
|
207 | + } |
|
208 | + |
|
209 | + try { |
|
210 | + $file = $this->folder->getFile($path); |
|
211 | + } catch (NotFoundException $e) { |
|
212 | + if ($size <= 0) { |
|
213 | + throw new NotFoundException; |
|
214 | + } |
|
215 | + |
|
216 | + if ($this->folder->fileExists('generated')) { |
|
217 | + $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
218 | + |
|
219 | + } else { |
|
220 | + $avatar = new OC_Image(); |
|
221 | + /** @var ISimpleFile $file */ |
|
222 | + $file = $this->folder->getFile('avatar.' . $ext); |
|
223 | + $avatar->loadFromData($file->getContent()); |
|
224 | + $avatar->resize($size); |
|
225 | + $data = $avatar->data(); |
|
226 | + } |
|
227 | + |
|
228 | + try { |
|
229 | + $file = $this->folder->newFile($path); |
|
230 | + $file->putContent($data); |
|
231 | + } catch (NotPermittedException $e) { |
|
232 | + $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
233 | + throw new NotFoundException(); |
|
234 | + } |
|
235 | + |
|
236 | + } |
|
237 | + |
|
238 | + if($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
239 | + $generated = $this->folder->fileExists('generated') ? 'true' : 'false'; |
|
240 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', $generated); |
|
241 | + } |
|
242 | + |
|
243 | + return $file; |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Get the extension of the avatar. If there is no avatar throw Exception |
|
248 | + * |
|
249 | + * @return string |
|
250 | + * @throws NotFoundException |
|
251 | + */ |
|
252 | + private function getExtension() { |
|
253 | + if ($this->folder->fileExists('avatar.jpg')) { |
|
254 | + return 'jpg'; |
|
255 | + } elseif ($this->folder->fileExists('avatar.png')) { |
|
256 | + return 'png'; |
|
257 | + } |
|
258 | + throw new NotFoundException; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * @param string $userDisplayName |
|
263 | + * @param int $size |
|
264 | + * @return string |
|
265 | + */ |
|
266 | + private function generateAvatar($userDisplayName, $size) { |
|
267 | + $text = mb_strtoupper(mb_substr($userDisplayName, 0, 1), 'UTF-8'); |
|
268 | + $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
269 | + |
|
270 | + $im = imagecreatetruecolor($size, $size); |
|
271 | + $background = imagecolorallocate($im, $backgroundColor->r, $backgroundColor->g, $backgroundColor->b); |
|
272 | + $white = imagecolorallocate($im, 255, 255, 255); |
|
273 | + imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
274 | + |
|
275 | + $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.ttf'; |
|
276 | + |
|
277 | + $fontSize = $size * 0.4; |
|
278 | + $box = imagettfbbox($fontSize, 0, $font, $text); |
|
279 | + |
|
280 | + $x = ($size - ($box[2] - $box[0])) / 2; |
|
281 | + $y = ($size - ($box[1] - $box[7])) / 2; |
|
282 | + $x += 1; |
|
283 | + $y -= $box[7]; |
|
284 | + imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
285 | + |
|
286 | + ob_start(); |
|
287 | + imagepng($im); |
|
288 | + $data = ob_get_contents(); |
|
289 | + ob_end_clean(); |
|
290 | + |
|
291 | + return $data; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Calculate steps between two Colors |
|
296 | + * @param object Color $steps start color |
|
297 | + * @param object Color $ends end color |
|
298 | + * @return array [r,g,b] steps for each color to go from $steps to $ends |
|
299 | + */ |
|
300 | + private function stepCalc($steps, $ends) { |
|
301 | + $step = array(); |
|
302 | + $step[0] = ($ends[1]->r - $ends[0]->r) / $steps; |
|
303 | + $step[1] = ($ends[1]->g - $ends[0]->g) / $steps; |
|
304 | + $step[2] = ($ends[1]->b - $ends[0]->b) / $steps; |
|
305 | + return $step; |
|
306 | + } |
|
307 | + /** |
|
308 | + * Convert a string to an integer evenly |
|
309 | + * @param string $hash the text to parse |
|
310 | + * @param int $maximum the maximum range |
|
311 | + * @return int between 0 and $maximum |
|
312 | + */ |
|
313 | + private function mixPalette($steps, $color1, $color2) { |
|
314 | + $count = $steps + 1; |
|
315 | + $palette = array($color1); |
|
316 | + $step = $this->stepCalc($steps, [$color1, $color2]); |
|
317 | + for ($i = 1; $i < $steps; $i++) { |
|
318 | + $r = intval($color1->r + ($step[0] * $i)); |
|
319 | + $g = intval($color1->g + ($step[1] * $i)); |
|
320 | + $b = intval($color1->b + ($step[2] * $i)); |
|
321 | + $palette[] = new Color($r, $g, $b); |
|
322 | + } |
|
323 | + return $palette; |
|
324 | + } |
|
325 | + |
|
326 | + |
|
327 | + /** |
|
328 | + * Convert a string to an integer evenly |
|
329 | + * @param string $hash the text to parse |
|
330 | + * @param int $maximum the maximum range |
|
331 | + * @return int between 0 and $maximum |
|
332 | + */ |
|
333 | + private function hashToInt($hash, $maximum) { |
|
334 | + $final = 0; |
|
335 | + $result = array(); |
|
336 | + |
|
337 | + // Splitting evenly the string |
|
338 | + for ($i=0; $i< strlen($hash); $i++) { |
|
339 | + // chars in md5 goes up to f, hex:16 |
|
340 | + $result[] = intval(substr($hash, $i, 1), 16) % 16; |
|
341 | + } |
|
342 | + // Adds up all results |
|
343 | + foreach ($result as $value) { |
|
344 | + $final += $value; |
|
345 | + } |
|
346 | + // chars in md5 goes up to f, hex:16 |
|
347 | + return intval($final % $maximum); |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * @param string $text |
|
353 | + * @return Color Object containting r g b int in the range [0, 255] |
|
354 | + */ |
|
355 | + function avatarBackgroundColor($text) { |
|
356 | + $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
357 | + |
|
358 | + $hash = md5($hash); |
|
359 | + $hashChars = str_split($hash); |
|
360 | + |
|
361 | + $red = new Color(182, 70, 157); |
|
362 | + $yellow = new Color(221, 203, 85); |
|
363 | + $blue = new Color(0, 130, 201); // Nextcloud blue |
|
364 | + // Number of steps to go from a color to another |
|
365 | + // 3 colors * 6 will result in 18 generated colors |
|
366 | + $steps = 6; |
|
367 | + |
|
368 | + $palette1 = $this->mixPalette($steps, $red, $yellow); |
|
369 | + $palette2 = $this->mixPalette($steps, $yellow, $blue); |
|
370 | + $palette3 = $this->mixPalette($steps, $blue, $red); |
|
371 | + |
|
372 | + $finalPalette = array_merge($palette1, $palette2, $palette3); |
|
373 | + |
|
374 | + return $finalPalette[$this->hashToInt($hash, $steps * 3 )]; |
|
375 | + } |
|
376 | + |
|
377 | + public function userChanged($feature, $oldValue, $newValue) { |
|
378 | + // We only change the avatar on display name changes |
|
379 | + if ($feature !== 'displayName') { |
|
380 | + return; |
|
381 | + } |
|
382 | + |
|
383 | + // If the avatar is not generated (so an uploaded image) we skip this |
|
384 | + if (!$this->folder->fileExists('generated')) { |
|
385 | + return; |
|
386 | + } |
|
387 | + |
|
388 | + $this->remove(); |
|
389 | + } |
|
390 | 390 | |
391 | 391 | } |
@@ -24,10 +24,10 @@ |
||
24 | 24 | namespace OC; |
25 | 25 | |
26 | 26 | class Color { |
27 | - public $r, $g, $b; |
|
28 | - public function __construct($r, $g, $b) { |
|
29 | - $this->r = $r; |
|
30 | - $this->g = $g; |
|
31 | - $this->b = $b; |
|
32 | - } |
|
27 | + public $r, $g, $b; |
|
28 | + public function __construct($r, $g, $b) { |
|
29 | + $this->r = $r; |
|
30 | + $this->g = $g; |
|
31 | + $this->b = $b; |
|
32 | + } |
|
33 | 33 | } |