userService = $userService; $this->fileService = $fileService; $this->httpHelper = $httpHelper; $this->thumbnailService = $thumbnailService; } public function registerRoutes(\Szurubooru\Router $router) { $router->get('/api/users/:userName/avatar/:size', [$this, 'getAvatarByName']); } public function getAvatarByName($userName, $size) { $user = $this->userService->getByName($userName); switch ($user->getAvatarStyle()) { case \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR: $hash = md5(strtolower(trim($user->getEmail() ? $user->getEmail() : $user->getId() . $user->getName()))); $url = 'https://www.gravatar.com/avatar/' . $hash . '?d=retro&s=' . $size; $this->serveFromUrl($url); break; case \Szurubooru\Entities\User::AVATAR_STYLE_BLANK: $this->serveFromFile($this->getBlankAvatarSourcePath(), $size); break; case \Szurubooru\Entities\User::AVATAR_STYLE_MANUAL: $this->serveFromFile($user->getCustomAvatarSourceContentPath(), $size); break; default: $this->serveFromFile($this->getBlankAvatarSourcePath(), $size); break; } } private function serveFromUrl($url) { $this->httpHelper->redirect($url); } private function serveFromFile($file, $size) { if (!$this->fileService->exists($file)) $file = $this->getBlankAvatarSourcePath(); $sizedFile = $this->thumbnailService->getOrGenerate($file, $size, $size); $this->fileService->serve($sizedFile); } private function getBlankAvatarSourcePath() { return 'avatars' . DIRECTORY_SEPARATOR . 'blank.png'; } }