Permalink
Browse files

fix Exception if idv1 or idv2 tag not found. add support idv2 tag APIC

  • Loading branch information...
1 parent cfdf0f2 commit 84dac1aef226384dc94b3dc7fd2ce623be6bb691 @Gemorroj committed Jan 20, 2013
Showing with 134 additions and 13 deletions.
  1. +15 −3 MP3/Id3/Best.php
  2. +22 −0 MP3/Id3/Id.php
  3. +18 −0 MP3/Id3/Idv2.php
  4. +59 −0 MP3/Id3/Picture.php
  5. +10 −5 README.md
  6. +10 −5 readme.txt
View
@@ -74,8 +74,18 @@ public function getMeta()
*/
public function read($file)
{
- $this->idv1->read($file);
- $this->idv2->read($file);
+ $idv1 = true;
+ $idv2 = true;
+ try {
+ $this->idv1->read($file);
+ } catch (Exception $idv1) {}
+ try {
+ $this->idv2->read($file);
+ } catch (Exception $idv2) {}
+
+ if ($idv1 !== true && $idv2 !== true) {
+ throw new MP3_Id3_Exception($idv1->getMessage() . "\n" . $idv2->getMessage());
+ }
$this->readTags();
$this->readMeta();
@@ -151,6 +161,7 @@ protected function readTags()
$this->setUrl($this->idv2->getUrl() ? $this->idv2->getUrl() : $this->idv1->getUrl());
$this->setEncodedBy($this->idv2->getEncodedBy() ? $this->idv2->getEncodedBy() : $this->idv1->getEncodedBy());
$this->setGenre($this->idv2->getGenre() ? $this->idv2->getGenre() : $this->idv1->getGenre());
+ $this->setPicture($this->idv2->getPicture() ? $this->idv2->getPicture() : $this->idv1->getPicture());
return $this;
}
@@ -187,7 +198,8 @@ protected function writeTags()
->setCopyright($this->getCopyright())
->setUrl($this->getUrl())
->setEncodedBy($this->getEncodedBy())
- ->setGenre($this->getGenre());
+ ->setGenre($this->getGenre())
+ ->setPicture($this->getPicture());
return $this;
}
View
@@ -27,6 +27,7 @@
protected $copyright;
protected $url;
protected $encodedBy;
+ protected $picture;
abstract public function write($file);
@@ -295,4 +296,25 @@ public function getAlbumArtist()
{
return $this->albumArtist;
}
+
+
+ /**
+ * @param MP3_Id3_Picture $picture
+ *
+ * @return MP3_Id3_Id
+ */
+ public function setPicture(MP3_Id3_Picture $picture = null)
+ {
+ $this->picture = $picture;
+
+ return $this;
+ }
+
+ /**
+ * @return MP3_Id3_Picture
+ */
+ public function getPicture()
+ {
+ return (null === $this->picture ? new MP3_Id3_Picture() : $this->picture);
+ }
}
View
@@ -16,8 +16,10 @@
require_once 'MP3/IDv2/Writer.php';
require_once 'MP3/Id3/Id.php';
require_once 'MP3/Id3/Genre.php';
+require_once 'MP3/Id3/Picture.php';
require_once 'MP3/Id3/Exception.php';
require_once 'MP3/IDv2/Tag.php';
+require_once 'MP3/IDv2/Frame/APIC.php';
require_once 'MP3/IDv2/Frame/TCON.php';
require_once 'MP3/IDv2/Frame/TRCK.php';
require_once 'MP3/IDv2/Frame/TIT2.php';
@@ -182,6 +184,15 @@ protected function writeTags()
$frame->addGenre($this->getGenre()->getId(), $this->getGenre()->getName());
$tag->addFrame($frame);
break;
+
+ case 'picture':
+ $frame = new MP3_IDv2_Frame_APIC();
+
+ $frame->setMimeType($this->getPicture()->getMime());
+ $frame->setPicture($this->getPicture()->getData());
+
+ $tag->addFrame($frame);
+ break;
}
}
@@ -247,6 +258,13 @@ protected function readTags()
$genre->setGenre($genres[0]);
$this->setGenre($genre);
break;
+
+ case 'APIC':
+ $picture = new MP3_Id3_Picture();
+ $picture->setMime($v->getMimeType());
+ $picture->setData($v->getPicture());
+ $this->setPicture($picture);
+ break;
}
}
View
@@ -0,0 +1,59 @@
+<?php
+/**
+ *
+ * This software is distributed under the GNU GPL v3.0 license.
+ *
+ * @author Gemorroj
+ * @copyright 2012 http://wapinet.ru
+ * @license http://www.gnu.org/licenses/gpl-3.0.txt
+ * @link https://github.com/Gemorroj/MP3_Id3
+ * @version 0.1 alpha
+ *
+ */
+
+class MP3_Id3_Picture
+{
+ protected $data;
+ protected $mime;
+
+
+ /**
+ * @param string $data
+ *
+ * @return MP3_Id3_Picture
+ */
+ public function setData($data)
+ {
+ $this->data = $data;
+
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getData()
+ {
+ return $this->data;
+ }
+
+ /**
+ * @param string $mime
+ *
+ * @return MP3_Id3_Picture
+ */
+ public function setMime($mime)
+ {
+ $this->mime = $mime;
+
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getMime()
+ {
+ return $this->mime;
+ }
+}
View
@@ -3,25 +3,30 @@
Пример:
```php
<?php
-set_include_path(__DIR__);
+set_include_path(__DIR__ . '/PEAR');
require_once 'MP3/Id3.php';
-$id3 = new MP3_Id3('./file.mp3');
+$id3 = new MP3_Id3('./file2.mp3');
$tags = $id3->getTags();
echo '<pre>';
+
print_r($id3->getMeta());
foreach ($tags as $k => $v) {
- print_r($k . ' - ' . $v);
+ if ($k === 'picture') {
+ echo '<img src="data:' . $v->getMime() . ';base64,' . base64_encode($v->getData()) . '" alt="picture" />';
+ } else {
+ print_r($k . ' - ' . print_r($v, true));
+ }
echo "\n";
}
$tags->setComment('test2');
$tags->setGenreId(23);
-$tags->setUrl('http://example.com');
+$tags->setUrl('http://wapinet.ru');
$tags->setAlbumArtist('test');
-$id3->write();
+//$id3->write();
echo '</pre>';
```
View
@@ -1,24 +1,29 @@
Работа с MP3 тегами.
Пример:
-set_include_path(__DIR__);
+set_include_path(__DIR__ . '/PEAR');
require_once 'MP3/Id3.php';
-$id3 = new MP3_Id3('./file.mp3');
+$id3 = new MP3_Id3('./file2.mp3');
$tags = $id3->getTags();
echo '<pre>';
+
print_r($id3->getMeta());
foreach ($tags as $k => $v) {
- print_r($k . ' - ' . $v);
+ if ($k === 'picture') {
+ echo '<img src="data:' . $v->getMime() . ';base64,' . base64_encode($v->getData()) . '" alt="picture" />';
+ } else {
+ print_r($k . ' - ' . print_r($v, true));
+ }
echo "\n";
}
$tags->setComment('test2');
$tags->setGenreId(23);
-$tags->setUrl('http://example.com');
+$tags->setUrl('http://wapinet.ru');
$tags->setAlbumArtist('test');
-$id3->write();
+//$id3->write();
echo '</pre>';

0 comments on commit 84dac1a

Please sign in to comment.