// save a playlist from serialised IDs if (@($idsCsv = $_REQUEST['save'])) { // convert to array of IDs $ids = explode(',', $idsCsv); // check each object exists and record it $objects = array(); foreach ($ids as $id) { if ($object = $index->getObject($id)) { $objects[] = $object; } else { throw new Exception('Invalid playlist items'); } } // save the playlist (list of ids), generating an ID 10 chars $playlistId = substr(md5($idsCsv), 0, 10); $store->set($playlistId, $objects); header('Content-Type:application/json'); echo json_encode(array('id' => $playlistId)); } elseif (@($id = $_REQUEST['load'])) { $objects = (array) $store->get($id); if (!$objects) { throw new Exception('Playlist not found'); } header('Content-Type:application/json'); echo json_encode($objects); } else { throw new Exception("specify a playlist ID to load=, or a list of song IDs to save="); } } catch (Exception $e) { // manually throw the error, as the json ext may not be loaded header('Content-Type:application/json');
private function assignAlbumArt() { $k = new keyStore('albumArt'); // generate a potential ID corresponding to this album/artist combination $id = md5($this->album . $this->artist); // check for existing art from the same album // if there, then assign this song that albumn ID if ($k->get($id)) { return $this->albumArtId = $id; } // get an instance of the ImageMagick class to manipulate // the album art image $im = new Imagick(); $blob = null; // look in the ID3v2 tag if (isset($this->analysis['id3v2']['APIC'][0]['data'])) { $blob =& $this->analysis['id3v2']['APIC'][0]['data']; } elseif (isset($this->analysis['id3v2']['PIC'][0]['data'])) { $blob =& $this->analysis['id3v2']['PIC'][0]['data']; } elseif ($images = glob($this->dir . '*.{jpg,png}', GLOB_BRACE)) { // use file pointers instead of file_get_contents // to fix a memory leak due to failed re-use of allocated memory // when loading successivle bigger files @($h = fopen($images[0], 'rb')); $size = filesize($images[0]); if ($h === false) { throw new Exception("Could not open cover art: {$images['0']}"); } if (!$size) { // invalid or no image //throw new Exception("Could not open cover art: $images[0]"); // assign no art return; } $blob = fread($h, $size); fclose($h); } else { // no albumn art available, assign none return; } // TODO, if necessary: try amazon web services // standardise the album art to 128x128 jpg $im->readImageBlob($blob); $im->thumbnailImage(128, 128); $im->setImageFormat('jpeg'); $im->setImageCompressionQuality(90); $blob = $im->getImageBlob(); // save the album art under the generated ID $k->set($id, $blob); // assign this song that albumn art ID $this->albumArtId = $id; }