Resize or make the avatar quadratic if required
authorCyperghost <olaf_schmitz_1@t-online.de>
Mon, 11 Nov 2024 12:16:56 +0000 (13:16 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Mon, 11 Nov 2024 12:16:56 +0000 (13:16 +0100)
wcfsetup/install/files/lib/system/worker/UserRebuildDataWorker.class.php

index 4bab50556f5065e262a0c7671c1a71ae2061cebf..25e4a8fed10f5beab513925f255114f85fe30e61 100644 (file)
@@ -15,7 +15,10 @@ use wcf\data\user\UserProfileAction;
 use wcf\data\user\UserProfileList;
 use wcf\system\bbcode\BBCodeHandler;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
+use wcf\system\exception\SystemException;
+use wcf\system\file\processor\UserAvatarFileProcessor;
 use wcf\system\html\input\HtmlInputProcessor;
+use wcf\system\image\ImageHandler;
 use wcf\system\user\storage\UserStorageHandler;
 use wcf\system\WCF;
 
@@ -230,6 +233,64 @@ final class UserRebuildDataWorker extends AbstractLinearRebuildDataWorker
                     continue;
                 }
 
+                $width = $avatar->width;
+                $height = $avatar->height;
+                if ($width != $height) {
+                    // make avatar quadratic
+                    $width = $height = \min($avatar->width, $avatar->height, UserAvatarFileProcessor::AVATAR_SIZE);
+                    $adapter = ImageHandler::getInstance()->getAdapter();
+
+                    try {
+                        $adapter->loadFile($avatar->getLocation());
+                    } catch (SystemException $e) {
+                        // broken image
+                        $editor->delete();
+                        continue;
+                    }
+
+                    $thumbnail = $adapter->createThumbnail($width, $height, false);
+                    $adapter->writeImage($thumbnail, $avatar->getLocation());
+                    // Clear thumbnail as soon as possible to free up the memory.
+                    $thumbnail = null;
+                }
+
+                if (
+                    $width != UserAvatarFileProcessor::AVATAR_SIZE
+                    && $width != UserAvatarFileProcessor::AVATAR_SIZE_2X
+                ) {
+                    // resize avatar
+                    $adapter = ImageHandler::getInstance()->getAdapter();
+
+                    try {
+                        $adapter->loadFile($avatar->getLocation());
+                    } catch (SystemException $e) {
+                        // broken image
+                        $editor->delete();
+                        continue;
+                    }
+
+                    if ($width > UserAvatarFileProcessor::AVATAR_SIZE_2X) {
+                        $adapter->resize(
+                            0,
+                            0,
+                            $width,
+                            $height,
+                            UserAvatarFileProcessor::AVATAR_SIZE_2X,
+                            UserAvatarFileProcessor::AVATAR_SIZE_2X
+                        );
+                    } else {
+                        $adapter->resize(
+                            0,
+                            0,
+                            $width,
+                            $height,
+                            UserAvatarFileProcessor::AVATAR_SIZE,
+                            UserAvatarFileProcessor::AVATAR_SIZE
+                        );
+                    }
+                    $adapter->writeImage($adapter->getImage(), $avatar->getLocation());
+                }
+
                 $file = FileEditor::createFromExistingFile(
                     $avatar->getLocation(),
                     $avatar->avatarName,