Skip to content

Commit d40d1df

Browse files
authored
Merge pull request #6313 from WoltLab/6.2-file-cover-photos
Cover photos based on file uploads
2 parents 0b05cf8 + d5905c1 commit d40d1df

5 files changed

Lines changed: 150 additions & 0 deletions

File tree

wcfsetup/install/files/lib/system/image/cover/photo/CoverPhotoImage.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @copyright 2001-2019 WoltLab GmbH
1212
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
1313
* @since 5.2
14+
* @deprecated 6.2 Use `ICoverPhoto` instead.
1415
*/
1516
class CoverPhotoImage
1617
{
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace wcf\system\image\cover\photo;
4+
5+
use wcf\system\style\StyleHandler;
6+
7+
/**
8+
* Represents the default cover photo as defined in the style configuration.
9+
*
10+
* @author Marcel Werk
11+
* @copyright 2001-2025 WoltLab GmbH
12+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13+
* @since 6.2
14+
*/
15+
final class DefaultCoverPhoto implements ICoverPhoto
16+
{
17+
private static DefaultCoverPhoto $defaultCoverPhoto;
18+
19+
/**
20+
* @var array{height: int, width: int}
21+
*/
22+
private array $dimensions;
23+
24+
private function __construct() {}
25+
26+
#[\Override]
27+
public function getUrl(?string $size = null): string
28+
{
29+
return StyleHandler::getInstance()->getStyle()->getCoverPhotoUrl();
30+
}
31+
32+
#[\Override]
33+
public function getWidth(?string $size = null): int
34+
{
35+
return $this->getDimensions()['width'];
36+
}
37+
38+
#[\Override]
39+
public function getHeight(?string $size = null): int
40+
{
41+
return $this->getDimensions()['height'];
42+
}
43+
44+
/**
45+
* @return array{height: int, width: int}
46+
*/
47+
private function getDimensions(): array
48+
{
49+
if (!isset($this->dimensions)) {
50+
$this->dimensions = ['height' => 0, 'width' => 0];
51+
$dimensions = @\getimagesize(
52+
StyleHandler::getInstance()->getStyle()->getCoverPhotoLocation()
53+
);
54+
if (\is_array($dimensions)) {
55+
$this->dimensions['width'] = $dimensions[0];
56+
$this->dimensions['height'] = $dimensions[1];
57+
}
58+
}
59+
60+
return $this->dimensions;
61+
}
62+
63+
public static function getDefaultCoverPhoto(): self
64+
{
65+
if (!isset(self::$defaultCoverPhoto)) {
66+
self::$defaultCoverPhoto = new self();
67+
}
68+
69+
return self::$defaultCoverPhoto;
70+
}
71+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace wcf\system\image\cover\photo;
4+
5+
use wcf\data\file\File;
6+
7+
/**
8+
* Represents a cover photo that is based on a file upload.
9+
*
10+
* @author Marcel Werk
11+
* @copyright 2001-2025 WoltLab GmbH
12+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13+
* @since 6.2
14+
*/
15+
final class FileCoverPhoto implements ICoverPhoto
16+
{
17+
public function __construct(private readonly File $file) {}
18+
19+
#[\Override]
20+
public function getUrl(?string $size = null): string
21+
{
22+
if ($size !== null) {
23+
$thumbnail = $this->file->getThumbnail($size);
24+
if ($thumbnail !== null) {
25+
return $thumbnail->getLink();
26+
}
27+
}
28+
29+
return $this->file->getFullSizeImageSource() ?: $this->file->getLink();
30+
}
31+
32+
#[\Override]
33+
public function getWidth(?string $size = null): int
34+
{
35+
if ($size !== null) {
36+
$thumbnail = $this->file->getThumbnail($size);
37+
if ($thumbnail !== null) {
38+
return $thumbnail->width;
39+
}
40+
}
41+
42+
return $this->file->width;
43+
}
44+
45+
#[\Override]
46+
public function getHeight(?string $size = null): int
47+
{
48+
if ($size !== null) {
49+
$thumbnail = $this->file->getThumbnail($size);
50+
if ($thumbnail !== null) {
51+
return $thumbnail->height;
52+
}
53+
}
54+
55+
return $this->file->height;
56+
}
57+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace wcf\system\image\cover\photo;
4+
5+
/**
6+
* Default interface for cover photos that support mulitple sizes.
7+
*
8+
* @author Marcel Werk
9+
* @copyright 2001-2025 WoltLab GmbH
10+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11+
* @since 6.2
12+
*/
13+
interface ICoverPhoto
14+
{
15+
public function getUrl(?string $size = null): string;
16+
17+
public function getWidth(?string $size = null): int;
18+
19+
public function getHeight(?string $size = null): int;
20+
}

wcfsetup/install/files/lib/system/image/cover/photo/ICoverPhotoImage.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @copyright 2001-2019 WoltLab GmbH
1010
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
1111
* @since 5.2
12+
* @deprecated 6.2 Use `ICoverPhoto` instead.
1213
*/
1314
interface ICoverPhotoImage
1415
{

0 commit comments

Comments
 (0)