metadata->get(['app', 'avatars', 'colorList']) ?? $this->colorList; $index = intval($x * count($colorList) / 128); return $colorList[$index]; } public function run(Request $request, Response $response): void { $userId = $request->getQueryParam('id'); $size = $request->getQueryParam('size') ?? null; if (!$userId) { throw new BadRequest(); } $user = $this->entityManager->getEntity('User', $userId); if (!$user) { $this->renderBlank($response); return; } $id = $user->get('avatarId'); if ($id) { $this->show($response, $id, $size, true); } $identicon = new Identicon(); if (!$size) { $size = 'small'; } if (empty($this->getSizes()[$size])) { $this->renderBlank($response); return; } $width = $this->getSizes()[$size][0]; $response ->setHeader('Cache-Control', 'max-age=360000, must-revalidate') ->setHeader('Content-Type', 'image/png'); $hash = $userId; $color = $this->getColor($userId); if ($hash === 'system') { $color = $this->metadata->get(['app', 'avatars', 'systemColor']) ?? $this->systemColor; } $imgContent = $identicon->getImageData($hash, $width, $color); $response->writeBody($imgContent); } protected function renderBlank(Response $response): void { ob_start(); $img = imagecreatetruecolor(14, 14); imagesavealpha($img, true); $color = imagecolorallocatealpha($img, 127, 127, 127, 127); imagefill($img, 0, 0, $color); imagepng($img); imagecolordeallocate($img, $color); $contents = ob_get_contents(); ob_end_clean(); imagedestroy($img); $response ->setHeader('Content-Type', 'image/png') ->writeBody($contents); } }