/src/Zend/Media/Id3/Frame/Apic.php

http://php-reader.googlecode.com/ · PHP · 290 lines · 122 code · 25 blank · 143 comment · 4 complexity · baf50c6cf2898e7f2662961b7e1ae0e5 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Media
  17. * @subpackage ID3
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Apic.php 241 2011-06-11 16:46:52Z svollbehr $
  21. */
  22. /**#@+ @ignore */
  23. require_once 'Zend/Media/Id3/Frame.php';
  24. require_once 'Zend/Media/Id3/Encoding.php';
  25. /**#@-*/
  26. /**
  27. * The <i>Attached picture</i> frame contains a picture directly related to the
  28. * audio file. Image format is the MIME type and subtype for the image.
  29. *
  30. * There may be several pictures attached to one file, each in their individual
  31. * APIC frame, but only one with the same content descriptor. There may only
  32. * be one picture with the same picture type.
  33. *
  34. * @category Zend
  35. * @package Zend_Media
  36. * @subpackage ID3
  37. * @author Sven Vollbehr <sven@vollbehr.eu>
  38. * @author Ryan Butterfield <buttza@gmail.com>
  39. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @version $Id: Apic.php 241 2011-06-11 16:46:52Z svollbehr $
  42. */
  43. final class Zend_Media_Id3_Frame_Apic extends Zend_Media_Id3_Frame
  44. implements Zend_Media_Id3_Encoding
  45. {
  46. /**
  47. * The list of image types.
  48. *
  49. * @var Array
  50. */
  51. public static $types = array
  52. ('Other', '32x32 pixels file icon (PNG only)', 'Other file icon',
  53. 'Cover (front)', 'Cover (back)', 'Leaflet page',
  54. 'Media (e.g. label side of CD)', 'Lead artist/lead performer/soloist',
  55. 'Artist/performer', 'Conductor', 'Band/Orchestra', 'Composer',
  56. 'Lyricist/text writer', 'Recording Location', 'During recording',
  57. 'During performance', 'Movie/video screen capture',
  58. 'A bright coloured fish', 'Illustration', 'Band/artist logotype',
  59. 'Publisher/Studio logotype');
  60. /** @var integer */
  61. private $_encoding;
  62. /** @var string */
  63. private $_mimeType = 'image/unknown';
  64. /** @var integer */
  65. private $_imageType = 0;
  66. /** @var string */
  67. private $_description;
  68. /** @var string */
  69. private $_imageData;
  70. /** @var integer */
  71. private $_imageSize = 0;
  72. /**
  73. * Constructs the class with given parameters and parses object related
  74. * data.
  75. *
  76. * @todo There is the possibility to put only a link to the image file by
  77. * using the MIME type '-->' and having a complete URL instead of picture
  78. * data. Support for such needs further design considerations.
  79. * @param Zend_Io_Reader $reader The reader object.
  80. * @param Array $options The options array.
  81. */
  82. public function __construct($reader = null, &$options = array())
  83. {
  84. parent::__construct($reader, $options);
  85. $this->setEncoding
  86. ($this->getOption('encoding', Zend_Media_Id3_Encoding::UTF8));
  87. if ($this->_reader === null) {
  88. return;
  89. }
  90. $encoding = $this->_reader->readUInt8();
  91. list($this->_mimeType) = $this->_explodeString8
  92. ($this->_reader->read($this->_reader->getSize()), 2);
  93. $this->_reader->setOffset(1 + strlen($this->_mimeType) + 1);
  94. $this->_imageType = $this->_reader->readUInt8();
  95. switch ($encoding) {
  96. case self::UTF16:
  97. // break intentionally omitted
  98. case self::UTF16BE:
  99. list ($this->_description, $this->_imageData) =
  100. $this->_explodeString16
  101. ($this->_reader->read($this->_reader->getSize()), 2);
  102. break;
  103. case self::UTF8:
  104. // break intentionally omitted
  105. default:
  106. list ($this->_description, $this->_imageData) =
  107. $this->_explodeString8
  108. ($this->_reader->read($this->_reader->getSize()), 2);
  109. break;
  110. }
  111. $this->_description =
  112. $this->_convertString($this->_description, $encoding);
  113. $this->_imageSize = strlen($this->_imageData);
  114. }
  115. /**
  116. * Returns the text encoding.
  117. *
  118. * All the strings read from a file are automatically converted to the
  119. * character encoding specified with the <var>encoding</var> option. See
  120. * {@link Zend_Media_Id3v2} for details. This method returns that character
  121. * encoding, or any value set after read, translated into a string form
  122. * regarless if it was set using a {@link Zend_Media_Id3_Encoding} constant
  123. * or a string.
  124. *
  125. * @return integer
  126. */
  127. public function getEncoding()
  128. {
  129. return $this->_translateIntToEncoding($this->_encoding);
  130. }
  131. /**
  132. * Sets the text encoding.
  133. *
  134. * All the string written to the frame are done so using given character
  135. * encoding. No conversions of existing data take place upon the call to
  136. * this method thus all texts must be given in given character encoding.
  137. *
  138. * The character encoding parameter takes either a
  139. * {@link Zend_Media_Id3_Encoding} constant or a character set name string
  140. * in the form accepted by iconv. The default character encoding used to
  141. * write the frame is 'utf-8'.
  142. *
  143. * @see Zend_Media_Id3_Encoding
  144. * @param integer $encoding The text encoding.
  145. */
  146. public function setEncoding($encoding)
  147. {
  148. $this->_encoding = $this->_translateEncodingToInt($encoding);
  149. }
  150. /**
  151. * Returns the MIME type. The MIME type is always ISO-8859-1 encoded.
  152. *
  153. * @return string
  154. */
  155. public function getMimeType()
  156. {
  157. return $this->_mimeType;
  158. }
  159. /**
  160. * Sets the MIME type. The MIME type is always ISO-8859-1 encoded.
  161. *
  162. * @param string $mimeType The MIME type.
  163. */
  164. public function setMimeType($mimeType)
  165. {
  166. $this->_mimeType = $mimeType;
  167. }
  168. /**
  169. * Returns the image type.
  170. *
  171. * @return integer
  172. */
  173. public function getImageType()
  174. {
  175. return $this->_imageType;
  176. }
  177. /**
  178. * Sets the image type code.
  179. *
  180. * @param integer $imageType The image type code.
  181. */
  182. public function setImageType($imageType)
  183. {
  184. $this->_imageType = $imageType;
  185. }
  186. /**
  187. * Returns the file description.
  188. *
  189. * @return string
  190. */
  191. public function getDescription()
  192. {
  193. return $this->_description;
  194. }
  195. /**
  196. * Sets the content description text using given encoding.
  197. *
  198. * @param string $description The content description text.
  199. * @param integer $encoding The text encoding.
  200. */
  201. public function setDescription($description, $encoding = null)
  202. {
  203. $this->_description = $description;
  204. if ($encoding !== null) {
  205. $this->setEncoding($encoding);
  206. }
  207. }
  208. /**
  209. * Returns the embedded image data.
  210. *
  211. * @return string
  212. */
  213. public function getImageData()
  214. {
  215. return $this->_imageData;
  216. }
  217. /**
  218. * Sets the embedded image data. Also updates the image size field to
  219. * correspond the new data.
  220. *
  221. * @param string $imageData The image data.
  222. */
  223. public function setImageData($imageData)
  224. {
  225. $this->_imageData = $imageData;
  226. $this->_imageSize = strlen($imageData);
  227. }
  228. /**
  229. * Returns the size of the embedded image data.
  230. *
  231. * @return integer
  232. */
  233. public function getImageSize()
  234. {
  235. return $this->_imageSize;
  236. }
  237. /**
  238. * Writes the frame raw data without the header.
  239. *
  240. * @param Zend_Io_Writer $writer The writer object.
  241. * @return void
  242. */
  243. protected function _writeData($writer)
  244. {
  245. $writer->writeUInt8($this->_encoding)
  246. ->writeString8($this->_mimeType, 1)
  247. ->writeUInt8($this->_imageType);
  248. switch ($this->_encoding) {
  249. case self::UTF16LE:
  250. $writer->writeString16
  251. ($this->_description,
  252. Zend_Io_Writer::LITTLE_ENDIAN_ORDER, 1);
  253. break;
  254. case self::UTF16:
  255. // break intentionally omitted
  256. case self::UTF16BE:
  257. $writer->writeString16($this->_description, null, 1);
  258. break;
  259. default:
  260. $writer->writeString8($this->_description, 1);
  261. break;
  262. }
  263. $writer->write($this->_imageData);
  264. }
  265. }