From 8035c3ef7617d424ea550a4197558c0ac2392d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ewilan=20Rivi=C3=A8re?= Date: Mon, 30 Sep 2024 21:03:28 +0200 Subject: [PATCH] Refactor Id3Writer class to add support for cover images --- src/Id3/Id3Writer.php | 49 ++++++++++++++++++++++++++----------------- tests/WriterTest.php | 1 + 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/Id3/Id3Writer.php b/src/Id3/Id3Writer.php index ff171fe..4b66525 100644 --- a/src/Id3/Id3Writer.php +++ b/src/Id3/Id3Writer.php @@ -5,6 +5,7 @@ use getid3_writetags; use Kiwilan\Audio\Audio; use Kiwilan\Audio\Core\AudioCore; +use Kiwilan\Audio\Core\AudioCoreCover; use Kiwilan\Audio\Enums\AudioFormatEnum; use Kiwilan\Audio\Enums\AudioTypeEnum; @@ -252,6 +253,16 @@ public function tag(string $key, string|int|bool|null $value): self return $this; } + /** + * @param string $pathOrData Path to cover image or binary data + */ + public function cover(string $pathOrData): self + { + $this->core->cover = AudioCoreCover::make($pathOrData); + + return $this; + } + /** * Set manually tags, to know which key used for which tag, you have to refer to documentation. * @@ -426,25 +437,25 @@ private function parseTagFormats(): self return $this; } - private function attachCover(array &$tags): void - { - $coverFormatsAllowed = [AudioFormatEnum::mp3]; - // if ($this->core->getCover() && in_array($this->audio->getFormat(), $coverFormatsAllowed)) { - // // $tags = [ - // // ...$tags, - // // 'CTOC' => $old_tags['id3v2']['CTOC'], - // // 'CHAP' => $old_tags['id3v2']['CHAP'], - // // 'chapters' => $old_tags['id3v2']['chapters'], - // // ]; - // $tags['attached_picture'][0] = [ - // 'data' => base64_decode($this->core->getCover()->data()), - // 'picturetypeid' => $this->core->getCover()->picturetypeid(), - // 'description' => $this->core->getCover()->description(), - // 'mime' => $this->core->getCover()->mime(), - // ]; - // $this->core->setHasCover(true); - // } - } + // private function attachCover(array &$tags): void + // { + // $coverFormatsAllowed = [AudioFormatEnum::mp3]; + // if ($this->core->cover && in_array($this->audio->getFormat(), $coverFormatsAllowed)) { + // // $tags = [ + // // ...$tags, + // // 'CTOC' => $old_tags['id3v2']['CTOC'], + // // 'CHAP' => $old_tags['id3v2']['CHAP'], + // // 'chapters' => $old_tags['id3v2']['chapters'], + // // ]; + // $tags['attached_picture'][0] = [ + // 'data' => base64_decode($this->core->cover->data), + // 'picturetypeid' => $this->core->cover->picture_type_id, + // 'description' => $this->core->cover->description, + // 'mime' => $this->core->cover->mime, + // ]; + // $this->core->has_cover = true; + // } + // } /** * @param array $tags diff --git a/tests/WriterTest.php b/tests/WriterTest.php index 8e2b865..9ed21d7 100644 --- a/tests/WriterTest.php +++ b/tests/WriterTest.php @@ -47,6 +47,7 @@ ->synopsis('New Synopsis') ->language('en') ->copyright('New Copyright') + ->cover(FOLDER) ->save(); $audio = Audio::read(MP3_WRITER);