|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\OpenAi\Service; |
| 9 | + |
| 10 | +use lsolesen\pel\PelEntryUndefined; |
| 11 | +use lsolesen\pel\PelExif; |
| 12 | +use lsolesen\pel\PelIfd; |
| 13 | +use lsolesen\pel\PelJpeg; |
| 14 | +use lsolesen\pel\PelTag; |
| 15 | +use lsolesen\pel\PelTiff; |
| 16 | +use OCP\ITempManager; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +class WatermarkingService { |
| 20 | + public const COMMENT = 'Generated with Artificial Intelligence'; |
| 21 | + public function __construct( |
| 22 | + private ITempManager $tempManager, |
| 23 | + private LoggerInterface $logger, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + public function markImage(string $image): string { |
| 28 | + try { |
| 29 | + $text = self::COMMENT; |
| 30 | + |
| 31 | + $img = imagecreatefromstring($image); |
| 32 | + $font = 5;// built-in font 1-5 |
| 33 | + $white = imagecolorallocate($img, 255, 255, 255); |
| 34 | + $black = imagecolorallocate($img, 0, 0, 0); |
| 35 | + |
| 36 | + $w = imagefontwidth($font) * strlen($text); |
| 37 | + $h = imagefontheight($font); |
| 38 | + $px = imagesx($img) - $w - 10; |
| 39 | + $py = imagesy($img) - $h - 10; |
| 40 | + |
| 41 | + // draw 1-pixel black outline by offsetting in 4 directions |
| 42 | + for ($dx = -1; $dx <= 1; $dx++) { |
| 43 | + for ($dy = -1; $dy <= 1; $dy++) { |
| 44 | + if ($dx || $dy) { |
| 45 | + imagestring($img, $font, $px + $dx, $py + $dy, $text, $black); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + imagestring($img, $font, $px, $py, $text, $white); |
| 50 | + |
| 51 | + $tempFile = $this->tempManager->getTemporaryFile('.jpg'); |
| 52 | + imagejpeg($img, $tempFile); |
| 53 | + imagedestroy($img); |
| 54 | + |
| 55 | + $newImage = $this->addImageExifComment($text, $tempFile); |
| 56 | + return $newImage; |
| 57 | + } catch (\Throwable $e) { |
| 58 | + $this->logger->warning('Could not add AI watermark to AI generated image', ['exception' => $e]); |
| 59 | + return $image; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private function addImageExifComment(string $text, string $filename): string { |
| 64 | + // Load PHP Exif Library for adding image metadata |
| 65 | + // Sadly PHPScoper was a nightmare with this dep, so here we go |
| 66 | + require_once(__DIR__ . '/../../vendor/fileeye/pel/autoload.php'); |
| 67 | + |
| 68 | + $peljpeg = new PelJpeg($filename); |
| 69 | + $exif = $peljpeg->getExif(); |
| 70 | + if (!$exif) { |
| 71 | + $exif = new PelExif(); |
| 72 | + $peljpeg->setExif($exif); |
| 73 | + } |
| 74 | + $peltiff = $exif->getTiff(); |
| 75 | + if (!$peltiff) { |
| 76 | + $peltiff = new PelTiff(); |
| 77 | + $exif->setTiff($peltiff); |
| 78 | + } |
| 79 | + $ifd = $peltiff->getIfd(); |
| 80 | + if (!$ifd) { |
| 81 | + $peltiff->setIfd(new PelIfd(PelIfd::IFD0)); |
| 82 | + $ifd = $peltiff->getIfd(); |
| 83 | + } |
| 84 | + |
| 85 | + $exifIfd = $ifd->getSubIfd(PelIfd::EXIF); |
| 86 | + if (!$exifIfd) { |
| 87 | + $exifIfd = new PelIfd(PelIfd::EXIF); |
| 88 | + $ifd->addSubIfd($exifIfd); |
| 89 | + } |
| 90 | + |
| 91 | + $comment = $exifIfd->getEntry(PelTag::USER_COMMENT); |
| 92 | + if (!$comment) { |
| 93 | + $comment = new PelEntryUndefined(PelTag::USER_COMMENT, $text); |
| 94 | + $exifIfd->addEntry($comment); |
| 95 | + } else { |
| 96 | + $comment->setValue($text); |
| 97 | + } |
| 98 | + |
| 99 | + return $peljpeg->getBytes(); |
| 100 | + } |
| 101 | + |
| 102 | + public function markAudio(string $audio): string { |
| 103 | + try { |
| 104 | + // Load getID3 library for adding audio metadata |
| 105 | + require_once(__DIR__ . '/../../vendor/james-heinrich/getid3/getid3/getid3.php'); |
| 106 | + |
| 107 | + $tempFile = $this->tempManager->getTemporaryFile('.mp3'); |
| 108 | + file_put_contents($tempFile, $audio); |
| 109 | + |
| 110 | + $getID3 = new \getID3; |
| 111 | + $getID3->setOption(['encoding' => 'UTF-8']); |
| 112 | + /** |
| 113 | + * @psalm-suppress UndefinedConstant |
| 114 | + */ |
| 115 | + \getid3_lib::IncludeDependency(GETID3_INCLUDEPATH . 'write.php', __FILE__, true); |
| 116 | + $tagwriter = new \getid3_writetags(); |
| 117 | + $tagwriter->filename = $tempFile; |
| 118 | + $tagwriter->tagformats = ['id3v2.4']; |
| 119 | + $tagwriter->tag_encoding = 'UTF-8'; |
| 120 | + $tagwriter->tag_data = ['comment' => [self::COMMENT]]; |
| 121 | + $tagwriter->WriteTags(); |
| 122 | + |
| 123 | + $newAudio = file_get_contents($tempFile); |
| 124 | + if (!$newAudio) { |
| 125 | + throw new \RuntimeException('Could not read temporary audio file'); |
| 126 | + } |
| 127 | + |
| 128 | + return $newAudio; |
| 129 | + } catch (\Throwable $e) { |
| 130 | + $this->logger->warning('Could not add AI watermark to AI generated image', ['exception' => $e]); |
| 131 | + return $audio; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments