Skip to content

Commit

Permalink
Refactor Id3Writer class to add support for cover images
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Sep 30, 2024
1 parent e150e9f commit 8035c3e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/Id3/Id3Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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<string, string> $tags
Expand Down
1 change: 1 addition & 0 deletions tests/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
->synopsis('New Synopsis')
->language('en')
->copyright('New Copyright')
->cover(FOLDER)
->save();

$audio = Audio::read(MP3_WRITER);
Expand Down

0 comments on commit 8035c3e

Please sign in to comment.