Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
avatarmanager.php
1<?php
3
5
6final class AvatarManager
7{
8 public const IMAGE_TYPE = 'image';
9 public const ICON_TYPE = 'icon';
10 public const COMMON_SPACE_AVATAR_ID = 'common-space';
11 public const AVATAR_SIZE = 300;
12
13 public function getImageAvatar(int $imageId): Avatar
14 {
15 $avatarId = $this->getAvatarImageUri($imageId);
16
17 return new Avatar(self::IMAGE_TYPE, $avatarId);
18 }
19
20 public function getIconAvatar(string $avatarId): Avatar
21 {
22 if ($avatarId !== self::COMMON_SPACE_AVATAR_ID)
23 {
24 $avatarId = Workgroup::getAvatarTypeWebCssClass($avatarId);
25 }
26
27 return new Avatar(self::ICON_TYPE, $avatarId);
28 }
29
30 private function getAvatarImageUri(int $imageId): string
31 {
32 $result = '';
33
34 $file = \CFile::getFileArray($imageId);
35 if (!empty($file))
36 {
37 $fileResized = \CFile::resizeImageGet(
38 $file,
39 [
40 'width' => 50,
41 'height' => 50,
42 ]
43 );
44
45 $result = Uri::urnEncode(htmlspecialcharsbx($fileResized['src']));
46 }
47
48 return $result;
49 }
50
56 public function loadAvatar(array $newPhotoFile): array
57 {
58 if (!(\CFile::checkImageFile($newPhotoFile, 0, 0, 0, ['IMAGE']) === null))
59 {
60 throw new \RuntimeException('The file is not an image.');
61 }
62
63 $newPhotoFile['MODULE_ID'] = 'socialnetwork';
64 $fileId = \CFile::saveFile($newPhotoFile, 'socialnetwork');
65 if (!$fileId)
66 {
67 throw new \RuntimeException('Unable to save file.');
68 }
69
70 $fileTmp = \CFile::resizeImageGet(
71 $fileId,
72 [
73 'width' => self::AVATAR_SIZE,
74 'height' => self::AVATAR_SIZE,
75 ],
76 BX_RESIZE_IMAGE_PROPORTIONAL,
77 false,
78 false,
79 true,
80 );
81
82 return [
83 'fileId' => $fileId,
84 'fileUri' => $fileTmp['src'],
85 ];
86 }
87
88 public function getAvatar(int $imageId): array
89 {
90 $fileId = \CFile::MakeFileArray($imageId);
91 \CFile::ResizeImage($fileId, ['width' => self::AVATAR_SIZE, 'height' => self::AVATAR_SIZE]);
92
93 return $fileId;
94 }
95}