diff options
Diffstat (limited to 'apps/theming/lib/ImageManager.php')
-rw-r--r-- | apps/theming/lib/ImageManager.php | 150 |
1 files changed, 84 insertions, 66 deletions
diff --git a/apps/theming/lib/ImageManager.php b/apps/theming/lib/ImageManager.php index f536bae0421..309bf192bc3 100644 --- a/apps/theming/lib/ImageManager.php +++ b/apps/theming/lib/ImageManager.php @@ -1,35 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Gary Kim <gary@garykim.dev> - * @author Jacob Neplokh <me@jacobneplokh.com> - * @author John Molakvoæ <skjnldsv@protonmail.com> - * @author Julien Veyssier <eneiluj@posteo.net> - * @author Julius Haertl <jus@bitgrid.net> - * @author Julius Härtl <jus@bitgrid.net> - * @author Michael Weimann <mail@michael-weimann.eu> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author ste101 <stephan_bauer@gmx.de> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\Theming; @@ -56,6 +29,7 @@ class ImageManager { private ICacheFactory $cacheFactory, private LoggerInterface $logger, private ITempManager $tempManager, + private BackgroundService $backgroundService, ) { } @@ -69,6 +43,9 @@ class ImageManager { $cacheBusterCounter = $this->config->getAppValue(Application::APP_ID, 'cachebuster', '0'); if ($this->hasImage($key)) { return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter; + } elseif ($key === 'backgroundDark' && $this->hasImage('background')) { + // Fall back to light variant + return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'background' ]) . '?v=' . $cacheBusterCounter; } switch ($key) { @@ -76,8 +53,17 @@ class ImageManager { case 'logoheader': case 'favicon': return $this->urlGenerator->imagePath('core', 'logo/logo.png') . '?v=' . $cacheBusterCounter; + case 'backgroundDark': case 'background': - return $this->urlGenerator->linkTo(Application::APP_ID, 'img/background/' . BackgroundService::DEFAULT_BACKGROUND_IMAGE); + // Removing the background defines its mime as 'backgroundColor' + $mimeSetting = $this->config->getAppValue('theming', 'backgroundMime', ''); + if ($mimeSetting !== 'backgroundColor') { + $image = BackgroundService::DEFAULT_BACKGROUND_IMAGE; + if ($key === 'backgroundDark') { + $image = BackgroundService::SHIPPED_BACKGROUNDS[$image]['dark_variant'] ?? $image; + } + return $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$image"); + } } return ''; } @@ -167,7 +153,7 @@ class ImageManager { * * @param string $filename * @throws NotFoundException - * @return \OCP\Files\SimpleFS\ISimpleFile + * @return ISimpleFile * @throws NotPermittedException */ public function getCachedImage(string $filename): ISimpleFile { @@ -180,7 +166,7 @@ class ImageManager { * * @param string $filename * @param string $data - * @return \OCP\Files\SimpleFS\ISimpleFile + * @return ISimpleFile * @throws NotFoundException * @throws NotPermittedException */ @@ -209,6 +195,10 @@ class ImageManager { } catch (NotFoundException $e) { } catch (NotPermittedException $e) { } + + if ($key === 'logo') { + $this->config->deleteAppValue('theming', 'logoDimensions'); + } } public function updateImage(string $key, string $tmpFile): string { @@ -227,51 +217,79 @@ class ImageManager { throw new \Exception('Unsupported image type: ' . $detectedMimeType); } - if ($key === 'background' && $this->shouldOptimizeBackgroundImage($detectedMimeType, filesize($tmpFile))) { - try { - // Optimize the image since some people may upload images that will be - // either to big or are not progressive rendering. - $newImage = @imagecreatefromstring(file_get_contents($tmpFile)); - if ($newImage === false) { - throw new \Exception('Could not read background image, possibly corrupted.'); - } + if ($key === 'background') { + if ($this->shouldOptimizeBackgroundImage($detectedMimeType, filesize($tmpFile))) { + try { + // Optimize the image since some people may upload images that will be + // either to big or are not progressive rendering. + $newImage = @imagecreatefromstring(file_get_contents($tmpFile)); + if ($newImage === false) { + throw new \Exception('Could not read background image, possibly corrupted.'); + } - // Preserve transparency - imagesavealpha($newImage, true); - imagealphablending($newImage, true); + // Preserve transparency + imagesavealpha($newImage, true); + imagealphablending($newImage, true); - $newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096); - $newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth)); - $outputImage = imagescale($newImage, $newWidth, $newHeight); - if ($outputImage === false) { - throw new \Exception('Could not scale uploaded background image.'); - } + $imageWidth = imagesx($newImage); + $imageHeight = imagesy($newImage); - $newTmpFile = $this->tempManager->getTemporaryFile(); - imageinterlace($outputImage, 1); - // Keep jpeg images encoded as jpeg - if (str_contains($detectedMimeType, 'image/jpeg')) { - if (!imagejpeg($outputImage, $newTmpFile, 90)) { - throw new \Exception('Could not recompress background image as JPEG'); + /** @var int */ + $newWidth = min(4096, $imageWidth); + $newHeight = intval($imageHeight / ($imageWidth / $newWidth)); + $outputImage = imagescale($newImage, $newWidth, $newHeight); + if ($outputImage === false) { + throw new \Exception('Could not scale uploaded background image.'); } - } else { - if (!imagepng($outputImage, $newTmpFile, 8)) { - throw new \Exception('Could not recompress background image as PNG'); + + $newTmpFile = $this->tempManager->getTemporaryFile(); + imageinterlace($outputImage, true); + // Keep jpeg images encoded as jpeg + if (str_contains($detectedMimeType, 'image/jpeg')) { + if (!imagejpeg($outputImage, $newTmpFile, 90)) { + throw new \Exception('Could not recompress background image as JPEG'); + } + } else { + if (!imagepng($outputImage, $newTmpFile, 8)) { + throw new \Exception('Could not recompress background image as PNG'); + } } - } - $tmpFile = $newTmpFile; - imagedestroy($outputImage); - } catch (\Exception $e) { - if (is_resource($outputImage) || $outputImage instanceof \GdImage) { + $tmpFile = $newTmpFile; imagedestroy($outputImage); - } + } catch (\Exception $e) { + if (isset($outputImage) && is_resource($outputImage) || $outputImage instanceof \GdImage) { + imagedestroy($outputImage); + } - $this->logger->debug($e->getMessage()); + $this->logger->debug($e->getMessage()); + } } + + // For background images we need to announce it + $this->backgroundService->setGlobalBackground($tmpFile); } $target->putContent(file_get_contents($tmpFile)); + if ($key === 'logo') { + $content = file_get_contents($tmpFile); + $newImage = @imagecreatefromstring($content); + if ($newImage !== false) { + $this->config->setAppValue('theming', 'logoDimensions', imagesx($newImage) . 'x' . imagesy($newImage)); + } elseif (str_starts_with($detectedMimeType, 'image/svg')) { + $matched = preg_match('/viewbox=["\']\d* \d* (\d*\.?\d*) (\d*\.?\d*)["\']/i', $content, $matches); + if ($matched) { + $this->config->setAppValue('theming', 'logoDimensions', $matches[1] . 'x' . $matches[2]); + } else { + $this->logger->warning('Could not read logo image dimensions to optimize for mail header'); + $this->config->deleteAppValue('theming', 'logoDimensions'); + } + } else { + $this->logger->warning('Could not read logo image dimensions to optimize for mail header'); + $this->config->deleteAppValue('theming', 'logoDimensions'); + } + } + return $detectedMimeType; } |