RepairLogoDimension.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Repair;
  8. use OCA\Theming\ImageManager;
  9. use OCP\IConfig;
  10. use OCP\Migration\IOutput;
  11. use OCP\Migration\IRepairStep;
  12. use OCP\Server;
  13. class RepairLogoDimension implements IRepairStep {
  14. public function __construct(
  15. protected IConfig $config,
  16. ) {
  17. }
  18. public function getName(): string {
  19. return 'Cache logo dimension to fix size in emails on Outlook';
  20. }
  21. public function run(IOutput $output): void {
  22. $logoDimensions = $this->config->getAppValue('theming', 'logoDimensions');
  23. if (preg_match('/^\d+x\d+$/', $logoDimensions)) {
  24. $output->info('Logo dimensions are already known');
  25. return;
  26. }
  27. try {
  28. /** @var ImageManager $imageManager */
  29. $imageManager = Server::get(ImageManager::class);
  30. } catch (\Throwable) {
  31. $output->info('Theming is disabled');
  32. return;
  33. }
  34. if (!$imageManager->hasImage('logo')) {
  35. $output->info('Theming is not used to provide a logo');
  36. return;
  37. }
  38. $simpleFile = $imageManager->getImage('logo', false);
  39. $image = @imagecreatefromstring($simpleFile->getContent());
  40. $dimensions = '';
  41. if ($image !== false) {
  42. $dimensions = imagesx($image) . 'x' . imagesy($image);
  43. } elseif (str_starts_with($simpleFile->getMimeType(), 'image/svg')) {
  44. $matched = preg_match('/viewbox=["\']\d* \d* (\d*\.?\d*) (\d*\.?\d*)["\']/i', $simpleFile->getContent(), $matches);
  45. if ($matched) {
  46. $dimensions = $matches[1] . 'x' . $matches[2];
  47. }
  48. }
  49. if (!$dimensions) {
  50. $output->warning('Failed to read dimensions from logo');
  51. $this->config->deleteAppValue('theming', 'logoDimensions');
  52. return;
  53. }
  54. $dimensions = imagesx($image) . 'x' . imagesy($image);
  55. $this->config->setAppValue('theming', 'logoDimensions', $dimensions);
  56. $output->info('Updated logo dimensions: ' . $dimensions);
  57. }
  58. }