Merge branch '5.3'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / cover / photo / UserCoverPhoto.class.php
1 <?php
2
3 namespace wcf\data\user\cover\photo;
4
5 use wcf\system\WCF;
6 use wcf\util\ImageUtil;
7
8 /**
9 * Represents a user's cover photo.
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
15 */
16 class UserCoverPhoto implements IUserCoverPhoto
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
30 /**
31 * @var int
32 */
33 protected $coverPhotoHasWebP = 0;
34
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 */
56 public function __construct($userID, $coverPhotoHash, $coverPhotoExtension, int $coverPhotoHasWebP)
57 {
58 $this->userID = $userID;
59 $this->coverPhotoHash = $coverPhotoHash;
60 $this->coverPhotoExtension = $coverPhotoExtension;
61 $this->coverPhotoHasWebP = $coverPhotoHasWebP;
62 }
63
64 /**
65 * @inheritDoc
66 */
67 public function delete()
68 {
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));
75 }
76 }
77
78 /**
79 * @inheritDoc
80 */
81 public function getLocation(?bool $forceWebP = null): string
82 {
83 return WCF_DIR . 'images/coverPhotos/' . $this->getFilename();
84 }
85
86 /**
87 * @inheritDoc
88 */
89 public function getURL(?bool $forceWebP = null): string
90 {
91 return WCF::getPath() . 'images/coverPhotos/' . $this->getFilename();
92 }
93
94 /**
95 * @inheritDoc
96 */
97 public function getFilename(?bool $forceWebP = null): string
98 {
99 $useWebP = $forceWebP || ($forceWebP === null && ImageUtil::browserSupportsWebP());
100
101 return \substr(
102 $this->coverPhotoHash,
103 0,
104 2
105 ) . '/' . $this->userID . '-' . $this->coverPhotoHash . '.' . ($useWebP ? 'webp' : $this->coverPhotoExtension);
106 }
107
108 /**
109 * Returns the minimum and maximum dimensions for cover photos.
110 *
111 * @return array
112 */
113 public static function getCoverPhotoDimensions()
114 {
115 return [
116 'max' => [
117 'height' => self::MAX_HEIGHT,
118 'width' => self::MAX_WIDTH,
119 ],
120 'min' => [
121 'height' => self::MIN_HEIGHT,
122 'width' => self::MIN_WIDTH,
123 ],
124 ];
125 }
126 }