[FEATURE] Introduce AvatarProvider
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / Avatar / ImageProvider.php
1 <?php
2 namespace TYPO3\CMS\Backend\Backend\Avatar;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Backend\Utility\BackendUtility;
17 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
18 use TYPO3\CMS\Core\Resource\ProcessedFile;
19 use TYPO3\CMS\Core\Resource\ResourceFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Class ImageProvider
24 */
25 class ImageProvider implements AvatarProviderInterface {
26
27 /**
28 * Get Image
29 *
30 * @param array $backendUser be_user record
31 * @param int $size
32 * @return Image|NULL
33 */
34 public function getImage(array $backendUser, $size) {
35 $fileUid = $this->getAvatarFileUid($backendUser['uid']);
36
37 // Get file object
38 try {
39 $file = ResourceFactory::getInstance()->getFileObject($fileUid);
40 $processedImage = $file->process(
41 ProcessedFile::CONTEXT_IMAGECROPSCALEMASK,
42 array('width' => $size . 'c', 'height' => $size . 'c')
43 );
44
45 $image = GeneralUtility::makeInstance(
46 Image::class,
47 $processedImage->getPublicUrl(TRUE),
48 $processedImage->getProperty('width'),
49 $processedImage->getProperty('height')
50 );
51
52 } catch (FileDoesNotExistException $e) {
53 // No image found
54 $image = NULL;
55 }
56
57 return $image;
58 }
59
60 /**
61 * Get Avatar fileUid
62 *
63 * @param int $beUserId
64 * @return int
65 */
66 protected function getAvatarFileUid($beUserId) {
67 $file = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
68 'uid_local',
69 'sys_file_reference',
70 'tablenames = \'be_users\' AND fieldname = \'avatar\' AND ' .
71 'table_local = \'sys_file\' AND uid_foreign = ' . (int)$beUserId .
72 BackendUtility::BEenableFields('sys_file_reference') . BackendUtility::deleteClause('sys_file_reference')
73 );
74 return $file ? $file['uid_local'] : 0;
75 }
76
77 /**
78 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
79 */
80 protected function getDatabaseConnection() {
81 return $GLOBALS['TYPO3_DB'];
82 }
83 }