From e8b4a64f8aa02f39bff555572c3865c587665317 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Thu, 15 May 2014 00:39:34 +0200 Subject: added possibility to upload a favicon --- plugins/CoreAdminHome/CustomLogo.php | 49 +++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'plugins/CoreAdminHome/CustomLogo.php') diff --git a/plugins/CoreAdminHome/CustomLogo.php b/plugins/CoreAdminHome/CustomLogo.php index ca845fe4fb..f4c95c49b3 100644 --- a/plugins/CoreAdminHome/CustomLogo.php +++ b/plugins/CoreAdminHome/CustomLogo.php @@ -17,6 +17,7 @@ class CustomLogo { const LOGO_HEIGHT = 300; const LOGO_SMALL_HEIGHT = 100; + const FAVICON_HEIGHT = 32; public function getLogoUrl($pathOnly = false) { @@ -129,6 +130,11 @@ class CustomLogo return self::rewritePath('misc/user/logo.png'); } + public static function getPathUserFavicon() + { + return self::rewritePath('misc/user/favicon.png'); + } + public static function getPathUserSvgLogo() { return self::rewritePath('misc/user/logo.svg'); @@ -146,20 +152,36 @@ class CustomLogo public function copyUploadedLogoToFilesystem() { + $uploadFieldName = 'customLogo'; + + $success = $this->uploadImage($uploadFieldName, self::LOGO_SMALL_HEIGHT, $this->getPathUserLogoSmall()); + $success = $success && $this->uploadImage($uploadFieldName, self::LOGO_HEIGHT, $this->getPathUserLogo()); + + return $success; + } + + public function copyUploadedFaviconToFilesystem() + { + $uploadFieldName = 'customFavicon'; + + return $this->uploadImage($uploadFieldName, self::FAVICON_HEIGHT, $this->getPathUserFavicon()); + } - if (empty($_FILES['customLogo']) - || !empty($_FILES['customLogo']['error']) + private function uploadImage($uploadFieldName, $targetHeight, $userPath) + { + if (empty($_FILES[$uploadFieldName]) + || !empty($_FILES[$uploadFieldName]['error']) ) { return false; } - $file = $_FILES['customLogo']['tmp_name']; + $file = $_FILES[$uploadFieldName]['tmp_name']; if (!file_exists($file)) { return false; } list($width, $height) = getimagesize($file); - switch ($_FILES['customLogo']['type']) { + switch ($_FILES[$uploadFieldName]['type']) { case 'image/jpeg': $image = imagecreatefromjpeg($file); break; @@ -173,30 +195,21 @@ class CustomLogo return false; } - $widthExpected = round($width * self::LOGO_HEIGHT / $height); - $smallWidthExpected = round($width * self::LOGO_SMALL_HEIGHT / $height); + $smallWidthExpected = round($width * $targetHeight / $height); - $logo = imagecreatetruecolor($widthExpected, self::LOGO_HEIGHT); - $logoSmall = imagecreatetruecolor($smallWidthExpected, self::LOGO_SMALL_HEIGHT); + $logoSmall = imagecreatetruecolor($smallWidthExpected, $targetHeight); // Handle transparency - $background = imagecolorallocate($logo, 0, 0, 0); $backgroundSmall = imagecolorallocate($logoSmall, 0, 0, 0); - imagecolortransparent($logo, $background); imagecolortransparent($logoSmall, $backgroundSmall); - if ($_FILES['customLogo']['type'] == 'image/png') { - imagealphablending($logo, false); + if ($_FILES[$uploadFieldName]['type'] == 'image/png') { imagealphablending($logoSmall, false); - imagesavealpha($logo, true); imagesavealpha($logoSmall, true); } - imagecopyresized($logo, $image, 0, 0, 0, 0, $widthExpected, self::LOGO_HEIGHT, $width, $height); - imagecopyresized($logoSmall, $image, 0, 0, 0, 0, $smallWidthExpected, self::LOGO_SMALL_HEIGHT, $width, $height); - - imagepng($logo, PIWIK_DOCUMENT_ROOT . '/' . $this->getPathUserLogo(), 3); - imagepng($logoSmall, PIWIK_DOCUMENT_ROOT . '/' . $this->getPathUserLogoSmall(), 3); + imagecopyresized($logoSmall, $image, 0, 0, 0, 0, $smallWidthExpected, $targetHeight, $width, $height); + imagepng($logoSmall, PIWIK_DOCUMENT_ROOT . '/' . $userPath, 3); return true; } -- cgit v1.2.3