PageRenderTime 22ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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