Commit | Line | Data |
---|---|---|
886f7f29 | 1 | <?php |
a9229942 | 2 | |
886f7f29 | 3 | namespace wcf\system\image\cover\photo; |
a9229942 | 4 | |
886f7f29 AE |
5 | use wcf\system\style\StyleHandler; |
6 | ||
7 | /** | |
8 | * Wrapper for cover photos with an automatic fallback to the global cover photo. | |
a9229942 | 9 | * |
886f7f29 AE |
10 | * @author Alexander Ebert |
11 | * @copyright 2001-2019 WoltLab GmbH | |
12 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
13 | * @package WoltLabSuite\Core\System\Image\Cover\Photo | |
14 | * @since 5.2 | |
15 | */ | |
a9229942 TD |
16 | class CoverPhotoImage |
17 | { | |
18 | /** | |
19 | * @var ICoverPhotoImage | |
20 | */ | |
21 | protected $coverPhotoImage; | |
22 | ||
23 | /** | |
24 | * @var int[] | |
25 | */ | |
26 | protected $dimensions; | |
27 | ||
28 | /** | |
29 | * @var ICoverPhotoImage | |
30 | */ | |
31 | protected static $defaultCoverPhotoImage; | |
32 | ||
33 | /** | |
34 | * @param ICoverPhotoImage|null $coverPhotoImage | |
35 | */ | |
36 | public function __construct(?ICoverPhotoImage $coverPhotoImage = null) | |
37 | { | |
38 | $this->coverPhotoImage = $coverPhotoImage ?: self::getDefaultCoverPhoto(); | |
39 | } | |
40 | ||
41 | /** | |
42 | * @return string | |
43 | */ | |
44 | public function getCaption() | |
45 | { | |
46 | return $this->coverPhotoImage->getCoverPhotoCaption(); | |
47 | } | |
48 | ||
49 | /** | |
50 | * @return int | |
51 | */ | |
52 | public function getHeight() | |
53 | { | |
54 | return $this->getDimensions()['height']; | |
55 | } | |
56 | ||
57 | /** | |
58 | * @return string | |
59 | */ | |
60 | public function getLocation() | |
61 | { | |
62 | return $this->coverPhotoImage->getCoverPhotoLocation(); | |
63 | } | |
64 | ||
65 | /** | |
66 | * @return string | |
67 | */ | |
68 | public function getUrl() | |
69 | { | |
70 | return $this->coverPhotoImage->getCoverPhotoUrl(); | |
71 | } | |
72 | ||
73 | /** | |
74 | * @return int | |
75 | */ | |
76 | public function getWidth() | |
77 | { | |
78 | return $this->getDimensions()['width']; | |
79 | } | |
80 | ||
81 | /** | |
82 | * @return int[] | |
83 | */ | |
84 | protected function getDimensions() | |
85 | { | |
86 | if ($this->dimensions === null) { | |
87 | $this->dimensions = ['height' => 0, 'width' => 0]; | |
88 | $dimensions = @\getimagesize($this->getLocation()); | |
89 | if (\is_array($dimensions)) { | |
90 | $this->dimensions['width'] = $dimensions[0]; | |
91 | $this->dimensions['height'] = $dimensions[1]; | |
92 | } | |
93 | } | |
94 | ||
95 | return $this->dimensions; | |
96 | } | |
97 | ||
98 | /** | |
99 | * @return ICoverPhotoImage | |
100 | */ | |
101 | protected static function getDefaultCoverPhoto() | |
102 | { | |
103 | if (self::$defaultCoverPhotoImage === null) { | |
104 | self::$defaultCoverPhotoImage = new class implements ICoverPhotoImage { | |
105 | public function getCoverPhotoCaption() | |
106 | { | |
107 | return ''; | |
108 | } | |
109 | ||
110 | public function getCoverPhotoLocation() | |
111 | { | |
112 | return StyleHandler::getInstance()->getStyle()->getCoverPhotoLocation(); | |
113 | } | |
114 | ||
115 | public function getCoverPhotoUrl() | |
116 | { | |
117 | return StyleHandler::getInstance()->getStyle()->getCoverPhotoUrl(); | |
118 | } | |
119 | }; | |
120 | } | |
121 | ||
122 | return self::$defaultCoverPhotoImage; | |
123 | } | |
886f7f29 | 124 | } |