Skip to content

Commit 26b9ee9

Browse files
committed
Resize or make the avatar quadratic if required
1 parent 4a6b290 commit 26b9ee9

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
use wcf\data\user\UserProfileList;
1616
use wcf\system\bbcode\BBCodeHandler;
1717
use wcf\system\database\util\PreparedStatementConditionBuilder;
18+
use wcf\system\exception\SystemException;
19+
use wcf\system\file\processor\UserAvatarFileProcessor;
1820
use wcf\system\html\input\HtmlInputProcessor;
21+
use wcf\system\image\ImageHandler;
1922
use wcf\system\user\storage\UserStorageHandler;
2023
use wcf\system\WCF;
2124

@@ -230,6 +233,64 @@ public function execute()
230233
continue;
231234
}
232235

236+
$width = $avatar->width;
237+
$height = $avatar->height;
238+
if ($width != $height) {
239+
// make avatar quadratic
240+
$width = $height = \min($avatar->width, $avatar->height, UserAvatarFileProcessor::AVATAR_SIZE);
241+
$adapter = ImageHandler::getInstance()->getAdapter();
242+
243+
try {
244+
$adapter->loadFile($avatar->getLocation());
245+
} catch (SystemException $e) {
246+
// broken image
247+
$editor->delete();
248+
continue;
249+
}
250+
251+
$thumbnail = $adapter->createThumbnail($width, $height, false);
252+
$adapter->writeImage($thumbnail, $avatar->getLocation());
253+
// Clear thumbnail as soon as possible to free up the memory.
254+
$thumbnail = null;
255+
}
256+
257+
if (
258+
$width != UserAvatarFileProcessor::AVATAR_SIZE
259+
&& $width != UserAvatarFileProcessor::AVATAR_SIZE_2X
260+
) {
261+
// resize avatar
262+
$adapter = ImageHandler::getInstance()->getAdapter();
263+
264+
try {
265+
$adapter->loadFile($avatar->getLocation());
266+
} catch (SystemException $e) {
267+
// broken image
268+
$editor->delete();
269+
continue;
270+
}
271+
272+
if ($width > UserAvatarFileProcessor::AVATAR_SIZE_2X) {
273+
$adapter->resize(
274+
0,
275+
0,
276+
$width,
277+
$height,
278+
UserAvatarFileProcessor::AVATAR_SIZE_2X,
279+
UserAvatarFileProcessor::AVATAR_SIZE_2X
280+
);
281+
} else {
282+
$adapter->resize(
283+
0,
284+
0,
285+
$width,
286+
$height,
287+
UserAvatarFileProcessor::AVATAR_SIZE,
288+
UserAvatarFileProcessor::AVATAR_SIZE
289+
);
290+
}
291+
$adapter->writeImage($adapter->getImage(), $avatar->getLocation());
292+
}
293+
233294
$file = FileEditor::createFromExistingFile(
234295
$avatar->getLocation(),
235296
$avatar->avatarName,

0 commit comments

Comments
 (0)