Skip to content

Commit c981969

Browse files
committed
Store the webp image as a base64 file
1 parent 686b5e5 commit c981969

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

com.woltlab.wcf/templates/shared_unfurlUrl.tpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
{else}
55
<div class="unfurlUrlCardContainer">
66
<div class="unfurlUrlCard{*
7-
*}{if URL_UNFURLING_SAVE_IMAGES && $object->hasCoverImage()} unfurlUrlCardCoverImage{/if}{*
8-
*}{if URL_UNFURLING_SAVE_IMAGES && $object->hasSquaredImage()} unfurlUrlCardSquaredImage{/if}{*
7+
*}{if $object->hasCoverImage()} unfurlUrlCardCoverImage{/if}{*
8+
*}{if $object->hasSquaredImage()} unfurlUrlCardSquaredImage{/if}{*
99
*}">
10-
{if URL_UNFURLING_SAVE_IMAGES && !$object->getImageUrl()|empty}
10+
{if $object->hasImageUrl()}
1111
<img src="{$object->getImageUrl()}" height="{$object->height}" width="{$object->width}" class="unfurlUrlImage" alt="" loading="lazy">
1212
{/if}
1313
<div class="unfurlUrlInformation">

wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ public function getHost(): string
110110
*/
111111
public function getImageUrl(): ?string
112112
{
113-
if ($this->isStored && $this->fileID) {
113+
if (URL_UNFURLING_SAVE_IMAGES && $this->isStored && $this->fileID !== null) {
114114
$file = FileRuntimeCache::getInstance()->getObject($this->fileID);
115115

116-
return $file?->getFullSizeImageSource();
116+
return 'data:image/webp;base64, ' . \file_get_contents($file->getPathname());
117117
} elseif (!empty($this->imageUrl)) {
118118
if (MODULE_IMAGE_PROXY) {
119119
$key = CryptoUtil::createSignedString($this->imageUrl);
@@ -129,14 +129,25 @@ public function getImageUrl(): ?string
129129
return null;
130130
}
131131

132+
public function hasImageUrl(): bool
133+
{
134+
if (URL_UNFURLING_SAVE_IMAGES && $this->isStored && $this->fileID !== null) {
135+
return true;
136+
} elseif (!empty($this->imageUrl)) {
137+
return true;
138+
}
139+
140+
return false;
141+
}
142+
132143
public function hasCoverImage(): bool
133144
{
134-
return $this->getImageType() === self::IMAGE_COVER && !empty($this->getImageUrl());
145+
return $this->getImageType() === self::IMAGE_COVER && $this->hasImageUrl();
135146
}
136147

137148
public function hasSquaredImage(): bool
138149
{
139-
return $this->getImageType() === self::IMAGE_SQUARED && !empty($this->getImageUrl());
150+
return $this->getImageType() === self::IMAGE_SQUARED && $this->hasImageUrl();
140151
}
141152

142153
public function isPlainUrl(): bool

wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrlEditor.class.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class UnfurlUrlEditor extends DatabaseObjectEditor
3232
public static $baseClass = UnfurlUrl::class;
3333

3434
/**
35-
* Creates a webp thumbnail for the given file.
35+
* Creates a webp thumbnail for the given file and saves it base64 encoded in a new `.bin` file.
3636
*/
37-
public static function createWebpThumbnail(string $file, string $originalFile): ?File
37+
public static function saveUnfurlImage(string $file, string $originalFile): ?File
3838
{
3939
$imageData = \getimagesize($file);
4040

@@ -43,6 +43,7 @@ public static function createWebpThumbnail(string $file, string $originalFile):
4343
return null;
4444
}
4545
$webpFile = FileUtil::getTemporaryFilename(extension: 'webp');
46+
$binFile = FileUtil::getTemporaryFilename(extension: 'bin');
4647

4748
try {
4849
$imageAdapter->loadFile($file);
@@ -52,9 +53,12 @@ public static function createWebpThumbnail(string $file, string $originalFile):
5253
// Clean up the thumbnail
5354
$thumbnail = null;
5455

56+
// Save the webp file as a base64 encoded binary file
57+
\file_put_contents($binFile, \base64_encode(\file_get_contents($webpFile)));
58+
5559
return FileEditor::createFromExistingFile(
56-
$webpFile,
57-
\pathinfo($originalFile, \PATHINFO_BASENAME) . ".webp",
60+
$binFile,
61+
\pathinfo($originalFile, \PATHINFO_BASENAME) . ".bin",
5862
'com.woltlab.wcf.unfurl'
5963
);
6064
} catch (SystemException | ImageNotReadable $e) {
@@ -74,6 +78,7 @@ public static function createWebpThumbnail(string $file, string $originalFile):
7478
} finally {
7579
// Clean up temporary files
7680
@\unlink($webpFile);
81+
@\unlink($binFile);
7782
}
7883
}
7984
}

wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ private function getImageData(UnfurlResponse $unfurlResponse): array
157157
'imageUrlHash' => \sha1($unfurlResponse->getImageUrl()),
158158
'fileID' => $file?->fileID,
159159
'isStored' => $file !== null ? 1 : 0,
160-
'width' => $file?->width ?? $imageData[0],
161-
'height' => $file?->height ?? $imageData[1],
160+
'width' => $imageData[0],
161+
'height' => $imageData[1],
162162
];
163163
} catch (UrlInaccessible | DownloadFailed $e) {
164164
return [];
@@ -225,7 +225,7 @@ private function createFile(array $imageData, string $originalFile, string $imag
225225
$tmp = FileUtil::getTemporaryFilename(extension: $extension);
226226
\file_put_contents($tmp, $image);
227227

228-
$file = UnfurlUrlEditor::createWebpThumbnail(
228+
$file = UnfurlUrlEditor::saveUnfurlImage(
229229
$tmp,
230230
\sprintf(
231231
"%s.%s",

wcfsetup/install/files/lib/system/worker/UnfurlUrlRebuildDataWorker.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function execute()
5656
continue;
5757
}
5858

59-
if (URL_UNFURLING_NO_IMAGES) {
59+
if (!URL_UNFURLING_SAVE_IMAGES) {
6060
// delete stored images
6161
if ($unfurlUrl->fileID !== null) {
6262
$deleteFileIDs[] = $unfurlUrl->fileID;
@@ -69,7 +69,7 @@ public function execute()
6969
} elseif ($unfurlUrl->fileID !== null) {
7070
$fileLocation = $this->getOldFileLocation($unfurlUrl);
7171

72-
$file = UnfurlUrlEditor::createWebpThumbnail(
72+
$file = UnfurlUrlEditor::saveUnfurlImage(
7373
$fileLocation,
7474
\pathinfo($unfurlUrl->imageUrl, PATHINFO_FILENAME)
7575
);

0 commit comments

Comments
 (0)