Commit | Line | Data |
---|---|---|
f3b2b889 | 1 | <?php |
a9229942 | 2 | |
f3b2b889 | 3 | namespace wcf\data\user\cover\photo; |
a9229942 | 4 | |
f3b2b889 | 5 | use wcf\system\WCF; |
5f118355 | 6 | use wcf\util\ImageUtil; |
f3b2b889 AE |
7 | |
8 | /** | |
9 | * Represents a user's cover photo. | |
a9229942 TD |
10 | * |
11 | * @author Alexander Ebert | |
12 | * @copyright 2001-2019 WoltLab GmbH | |
13 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
14 | * @package WoltLabSuite\Core\Data\User\Cover\Photo | |
f3b2b889 | 15 | */ |
25f41e0c | 16 | class UserCoverPhoto implements IWebpUserCoverPhoto |
a9229942 TD |
17 | { |
18 | /** | |
19 | * file extension | |
20 | * @var string | |
21 | */ | |
22 | protected $coverPhotoExtension; | |
23 | ||
24 | /** | |
25 | * file hash | |
26 | * @var string | |
27 | */ | |
28 | protected $coverPhotoHash; | |
29 | ||
d4cf0997 AE |
30 | /** |
31 | * @var int | |
32 | */ | |
33 | protected $coverPhotoHasWebP = 0; | |
34 | ||
a9229942 TD |
35 | /** |
36 | * user id | |
37 | * @var int | |
38 | */ | |
39 | protected $userID; | |
40 | ||
41 | const MAX_HEIGHT = 800; | |
42 | ||
43 | const MAX_WIDTH = 2000; | |
44 | ||
45 | const MIN_HEIGHT = 200; | |
46 | ||
47 | const MIN_WIDTH = 500; | |
48 | ||
49 | /** | |
50 | * UserCoverPhoto constructor. | |
51 | * | |
52 | * @param int $userID | |
53 | * @param string $coverPhotoHash | |
54 | * @param string $coverPhotoExtension | |
55 | */ | |
d4cf0997 | 56 | public function __construct($userID, $coverPhotoHash, $coverPhotoExtension, int $coverPhotoHasWebP) |
a9229942 TD |
57 | { |
58 | $this->userID = $userID; | |
59 | $this->coverPhotoHash = $coverPhotoHash; | |
60 | $this->coverPhotoExtension = $coverPhotoExtension; | |
d4cf0997 | 61 | $this->coverPhotoHasWebP = $coverPhotoHasWebP; |
a9229942 TD |
62 | } |
63 | ||
64 | /** | |
65 | * @inheritDoc | |
66 | */ | |
67 | public function delete() | |
68 | { | |
5f118355 AE |
69 | if (\file_exists($this->getLocation(false))) { |
70 | @\unlink($this->getLocation(false)); | |
71 | } | |
72 | ||
73 | if (\file_exists($this->getLocation(true))) { | |
74 | @\unlink($this->getLocation(true)); | |
a9229942 TD |
75 | } |
76 | } | |
77 | ||
78 | /** | |
79 | * @inheritDoc | |
80 | */ | |
d4cf0997 | 81 | public function getLocation(?bool $forceWebP = null): string |
a9229942 TD |
82 | { |
83 | return WCF_DIR . 'images/coverPhotos/' . $this->getFilename(); | |
84 | } | |
85 | ||
86 | /** | |
87 | * @inheritDoc | |
88 | */ | |
d4cf0997 | 89 | public function getURL(?bool $forceWebP = null): string |
a9229942 TD |
90 | { |
91 | return WCF::getPath() . 'images/coverPhotos/' . $this->getFilename(); | |
92 | } | |
93 | ||
94 | /** | |
95 | * @inheritDoc | |
96 | */ | |
d4cf0997 | 97 | public function getFilename(?bool $forceWebP = null): string |
a9229942 | 98 | { |
5f118355 AE |
99 | $useWebP = $forceWebP || ($forceWebP === null && ImageUtil::browserSupportsWebP()); |
100 | ||
a9229942 TD |
101 | return \substr( |
102 | $this->coverPhotoHash, | |
103 | 0, | |
104 | 2 | |
5f118355 | 105 | ) . '/' . $this->userID . '-' . $this->coverPhotoHash . '.' . ($useWebP ? 'webp' : $this->coverPhotoExtension); |
a9229942 TD |
106 | } |
107 | ||
25f41e0c AE |
108 | /** |
109 | * @inheritDoc | |
110 | */ | |
111 | public function createWebpVariant() | |
112 | { | |
113 | if ($this->coverPhotoHasWebP) { | |
114 | return; | |
115 | } | |
116 | ||
117 | $sourceLocation = WCF_DIR . $this->getFilename($this->coverPhotoExtension === 'webp'); | |
118 | $outputFilenameWithoutExtension = \preg_replace('~\.[a-z]+$~', '', $sourceLocation); | |
119 | ||
120 | return ImageUtil::createWebpVariant($sourceLocation, $outputFilenameWithoutExtension); | |
121 | } | |
122 | ||
a9229942 TD |
123 | /** |
124 | * Returns the minimum and maximum dimensions for cover photos. | |
125 | * | |
126 | * @return array | |
127 | */ | |
128 | public static function getCoverPhotoDimensions() | |
129 | { | |
130 | return [ | |
131 | 'max' => [ | |
132 | 'height' => self::MAX_HEIGHT, | |
133 | 'width' => self::MAX_WIDTH, | |
134 | ], | |
135 | 'min' => [ | |
136 | 'height' => self::MIN_HEIGHT, | |
137 | 'width' => self::MIN_WIDTH, | |
138 | ], | |
139 | ]; | |
140 | } | |
f3b2b889 | 141 | } |