‪TYPO3CMS  ‪main
DefaultAvatarProvider.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
24 
30 {
38  public function ‪getImage(array $backendUser, $size)
39  {
40  $fileUid = $this->‪getAvatarFileUid($backendUser['uid']);
41  if ($fileUid === 0) {
42  // Early return if there is no valid image file UID
43  return null;
44  }
45  // Get file object
46  try {
47  $file = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($fileUid);
48  $processedImage = $file->process(
50  ['width' => $size . 'c', 'height' => $size . 'c']
51  );
52 
53  ‪$publicUrl = $processedImage->getPublicUrl();
54  if (‪$publicUrl) {
55  $image = GeneralUtility::makeInstance(
56  Image::class,
58  $processedImage->getProperty('width'),
59  $processedImage->getProperty('height')
60  );
61  } else {
62  $image = null;
63  }
64  } catch (‪FileDoesNotExistException $e) {
65  // No image found
66  $image = null;
67  }
68 
69  return $image;
70  }
71 
78  protected function ‪getAvatarFileUid($backendUserId)
79  {
80  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
81  $fileUid = $queryBuilder
82  ->select('uid_local')
83  ->from('sys_file_reference')
84  ->where(
85  $queryBuilder->expr()->eq(
86  'tablenames',
87  $queryBuilder->createNamedParameter('be_users')
88  ),
89  $queryBuilder->expr()->eq(
90  'fieldname',
91  $queryBuilder->createNamedParameter('avatar')
92  ),
93  $queryBuilder->expr()->eq(
94  'uid_foreign',
95  $queryBuilder->createNamedParameter((int)$backendUserId, ‪Connection::PARAM_INT)
96  )
97  )
98  ->executeQuery()
99  ->fetchOne();
100 
101  return (int)$fileUid;
102  }
103 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGECROPSCALEMASK
‪const CONTEXT_IMAGECROPSCALEMASK
Definition: ProcessedFile.php:61
‪TYPO3\CMS\Backend\Backend\Avatar\DefaultAvatarProvider\getImage
‪Image null getImage(array $backendUser, $size)
Definition: DefaultAvatarProvider.php:38
‪TYPO3\CMS\Backend\Backend\Avatar\DefaultAvatarProvider
Definition: DefaultAvatarProvider.php:30
‪TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
Definition: FileDoesNotExistException.php:21
‪TYPO3\CMS\Webhooks\Message\$publicUrl
‪identifier readonly string readonly string $publicUrl
Definition: FileUpdatedMessage.php:36
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Backend\Backend\Avatar
Definition: Avatar.php:18
‪TYPO3\CMS\Backend\Backend\Avatar\DefaultAvatarProvider\getAvatarFileUid
‪int getAvatarFileUid($backendUserId)
Definition: DefaultAvatarProvider.php:78
‪TYPO3\CMS\Backend\Backend\Avatar\AvatarProviderInterface
Definition: AvatarProviderInterface.php:22
‪TYPO3\CMS\Backend\Backend\Avatar\Image
Definition: Image.php:23
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52