[CLEANUP] Streamline Avatar-related code in EXT:backend
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Backend / Avatar / Image.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
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Utility\PathUtility;
19
20 /**
21 * Acts as a pseudo model for holding all information of an avatar image
22 * Holds url + dimensions of avatar image
23 */
24 class Image
25 {
26 /**
27 * Url of avatar image. Needs to be relative to the website root or an absolute URL.
28 *
29 * @var string
30 */
31 protected $url;
32
33 /**
34 * @var int
35 */
36 protected $width;
37
38 /**
39 * @var int
40 */
41 protected $height;
42
43 /**
44 * @param string $url url of image. Needs to be relative to the website root or an absolute URL.
45 * @param int $width width of image
46 * @param int $height height of image
47 */
48 public function __construct($url, $width, $height)
49 {
50 $this->url = $url;
51 $this->width = (int)$width;
52 $this->height = (int)$height;
53 }
54
55 /**
56 * Fetches the URL to the the avatar image
57 *
58 * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all.
59 * @return string
60 */
61 public function getUrl($relativeToCurrentScript = false)
62 {
63 $url = $this->url;
64
65 if ($relativeToCurrentScript && !GeneralUtility::isValidUrl($url)) {
66 $absolutePathToContainingFolder = PathUtility::dirname(PATH_site . $url);
67 $pathPart = PathUtility::getRelativePathTo($absolutePathToContainingFolder);
68 $filePart = substr(PATH_site . $url, strlen($absolutePathToContainingFolder) + 1);
69 $url = $pathPart . $filePart;
70 }
71 return $url;
72 }
73
74 /**
75 * @return int
76 */
77 public function getWidth()
78 {
79 return $this->width;
80 }
81
82 /**
83 * @return int
84 */
85 public function getHeight()
86 {
87 return $this->height;
88 }
89 }