aboutsummaryrefslogtreecommitdiffstats
path: root/apps/testing/lib/Conversion/ConversionProvider.php
blob: b8d93428694642ede67fbd77fc37a7410a78ae8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\Testing\Conversion;

use OCP\Files\Conversion\ConversionMimeProvider;
use OCP\Files\Conversion\IConversionProvider;
use OCP\Files\File;
use OCP\IL10N;

class ConversionProvider implements IConversionProvider {
	public function __construct(
		private IL10N $l10n,
	) {
	}

	public function getSupportedMimeTypes(): array {
		return [
			new ConversionMimeProvider('image/jpeg', 'image/png', 'png', $this->l10n->t('Image (.png)')),
			new ConversionMimeProvider('image/jpeg', 'image/gif', 'gif', $this->l10n->t('Image (.gif)')),
		];
	}

	public function convertFile(File $file, string $targetMimeType): mixed {
		$image = imagecreatefromstring($file->getContent());
		imagepalettetotruecolor($image);

		// Start output buffering
		ob_start();

		// Convert the image to the target format
		if ($targetMimeType === 'image/gif') {
			imagegif($image);
		} else {
			imagepng($image);
		}

		// End and return the output buffer
		return ob_get_clean();
	}
}