public static function getAvatar(\User $user, $res) { global $wgDefaultAvatar, $wgDefaultAvatarRes; $path = $wgDefaultAvatar; // If user exists if ($user && $user->getId()) { global $wgUploadDirectory, $wgUploadPath; $avatarPath = "/avatars/{$user->getId()}/{$res}.png"; // Check if requested avatar thumbnail exists if (file_exists($wgUploadDirectory . $avatarPath)) { $path = $wgUploadPath . $avatarPath; } else { if ($res !== 'original') { // Dynamically generate upon request $originalAvatarPath = "/avatars/{$user->getId()}/original.png"; if (file_exists($wgUploadDirectory . $originalAvatarPath)) { $image = Thumbnail::open($wgUploadDirectory . $originalAvatarPath); $image->createThumbnail($res, $wgUploadDirectory . $avatarPath); $image->cleanup(); $path = $wgUploadPath . $avatarPath; } } } } return $path; }
private function processUpload() { $request = $this->getRequest(); $dataurl = $request->getVal('avatar'); if (!$dataurl || parse_url($dataurl, PHP_URL_SCHEME) !== 'data') { $this->displayMessage($this->msg('avatar-notuploaded')); return false; } $img = Thumbnail::open($dataurl); global $wgMaxAvatarResolution; switch ($img->type) { case IMAGETYPE_GIF: case IMAGETYPE_PNG: case IMAGETYPE_JPEG: break; default: $this->displayMessage($this->msg('avatar-invalid')); return false; } // Must be square if ($img->width !== $img->height) { $this->displayMessage($this->msg('avatar-notsquare')); return false; } // Check if image is too small if ($img->width < 32 || $img->height < 32) { $this->displayMessage($this->msg('avatar-toosmall')); return false; } // Check if image is too big if ($img->width > $wgMaxAvatarResolution || $img->height > $wgMaxAvatarResolution) { $this->displayMessage($this->msg('avatar-toolarge')); return false; } $user = $this->getUser(); Avatars::deleteAvatar($user); // Avatar directories global $wgAvatarUploadDirectory; $uploadDir = $wgAvatarUploadDirectory . '/' . $this->getUser()->getId() . '/'; @mkdir($uploadDir, 0777, true); // We do this to convert format to png $img->createThumbnail($wgMaxAvatarResolution, $uploadDir . 'original.png'); // We only create thumbnail with default resolution here. Others are generated on demand global $wgDefaultAvatarRes; $img->createThumbnail($wgDefaultAvatarRes, $uploadDir . $wgDefaultAvatarRes . '.png'); $img->cleanup(); $this->displayMessage($this->msg('avatar-saved')); global $wgAvatarLogInRC; $logEntry = new \ManualLogEntry('avatar', 'upload'); $logEntry->setPerformer($this->getUser()); $logEntry->setTarget($this->getUser()->getUserPage()); $logId = $logEntry->insert(); $logEntry->publish($logId, $wgAvatarLogInRC ? 'rcandudp' : 'udp'); return true; }