Search by

sherotree / mp3-cover-adder

sherotree

PHP library to embed album art into MP3 files and edit ID3 tags

0.1.0 2026-11-29 00:58 UTC

This package is not auto-updated.

Last update: 2026-11-30 00:06:44 UTC


README

Add Cover to MP3

A PHP library to embed album art into MP3 files and edit ID3 tags (title, artist, album). All operations are performed locally without requiring network access.

Features

  • Read ID3 Metadata: Extract title, artist, and album from MP3 files
  • Read Album Art: Access existing embedded cover images
  • Set Album Art: Embed JPEG, PNG, or GIF images as album art
  • Remove Album Art: Strip existing cover images from MP3 files
  • Edit Metadata: Update title, artist, and album fields
  • Save Changes: Write updated MP3 files to disk or get as bytes
  • Chainable API: Fluent interface for method chaining
  • Offline First: No network dependencies, all processing is local

Requirements

  • PHP 8.1 or higher
  • Composer

Installation

Install via Composer:

composer require sherotree/mp3-cover-adder

Usage

Basic Example

<?php

use Sherotree\Mp3CoverAdder\Mp3CoverAdder;

// Load an MP3 file
$mp3 = new Mp3CoverAdder('path/to/song.mp3');

// Set metadata and cover art
$mp3->setTitle('My Song')
    ->setArtist('My Artist')
    ->setAlbum('My Album')
    ->setCoverFromFile('path/to/cover.jpg')
    ->save();

Reading Metadata

$mp3 = new Mp3CoverAdder('song.mp3');

echo $mp3->getTitle();  // Current title
echo $mp3->getArtist(); // Current artist
echo $mp3->getAlbum();  // Current album

// Check if cover art exists
if ($mp3->hasCover()) {
    $coverData = $mp3->getCoverData(); // Raw image bytes
    $coverMime = $mp3->getCoverMime(); // e.g., 'image/jpeg'
}

Setting Album Art

// From a file
$mp3->setCoverFromFile('cover.jpg')->save();

// From raw image data
$imageData = file_get_contents('cover.png');
$mp3->setCover($imageData)->save();

// With explicit MIME type
$mp3->setCover($imageData, 'image/png')->save();

Removing Album Art

$mp3->removeCover()->save();

Saving Changes

// Save to the original file
$mp3->save();

// Save to a new file
$mp3->save('path/to/output.mp3');

// Get as bytes without saving to disk
$mp3Data = $mp3->getBytes();
file_put_contents('output.mp3', $mp3Data);

Complete Example

use Sherotree\Mp3CoverAdder\Mp3CoverAdder;

$mp3 = new Mp3CoverAdder('input.mp3');

// Update all metadata
$mp3->setTitle('Summer Nights')
    ->setArtist('The Band')
    ->setAlbum('Greatest Hits 2026')
    ->setCoverFromFile('album-cover.jpg')
    ->save('output.mp3');

echo "Updated MP3 saved successfully!\n";

API Reference

Constructor

new Mp3CoverAdder(string $filePath)

Creates a new instance and loads the specified MP3 file.

Throws:

  • InvalidArgumentException if the file doesn't exist or isn't readable

Reading Methods

getTitle(): string
getArtist(): string
getAlbum(): string
getCoverData(): ?string
getCoverMime(): ?string
hasCover(): bool

Writing Methods

All setter methods return $this for method chaining:

setTitle(string $title): self
setArtist(string $artist): self
setAlbum(string $album): self
setCover(string $imageData, ?string $mimeType = null): self
setCoverFromFile(string $imagePath): self
removeCover(): self

Saving Methods

save(?string $outputPath = null): bool
getBytes(): string

Supported Image Formats

  • JPEG (.jpg, .jpeg)
  • PNG (.png)
  • GIF (.gif)

Testing

Run the test suite:

composer install
composer test

License

MIT License. See LICENSE for details.

Author

Sherotree
GitHub: @sherotree

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Related