MPoD 용 이미지파일 추출 스크립트

Note 의견 남기기

임시로 대충 짜집기하여 만든것.

<?php
require_once('getID3/getid3/getid3.php');
 
$folderName = "/var/www/Music/".urldecode(dirname($_SERVER['QUERY_STRING']));
 
$d = dir($folderName);
while(false !== ($entry = $d->read())) {
	if( $entry == '.' || $entry == '..' ) continue;
	if( isMusicFile($entry) ) {
		getImage($folderName.'/'.$entry);
		$d->close();
		exit;
	}
}
$d->close();
 
function getImage($filename) {
	$getID3 = new GetID3;
	$getID3->option_tag_id3v2 = true; # Don't know what this does yet 	
	$getID3->option_tags_images = true;
	$getID3->analyze($filename);
 
	$cover = null;
	$mimetype = null;
 
	if (isset($getID3->info['id3v2']['APIC'][0]['data'])) {
	    $cover = $getID3->info['id3v2']['APIC'][0]['data'];
	} elseif (isset($getID3->info['id3v2']['PIC'][0]['data'])) {
	    $cover = $getID3->info['id3v2']['PIC'][0]['data'];
	} elseif (isset($getID3->info['comments']['picture'][0]['data'])) { // for m4a 
		$cover = $getID3->info['comments']['picture'][0]['data'];
		$mimetype = $getID3->info['comments']['picture'][0]['image_mime'];
	}
 
	if( !$mimetype ) {
		if (isset($getID3->info['id3v2']['APIC'][0]['image_mime'])) {
		    $mimetype = $getID3->info['id3v2']['APIC'][0]['image_mime'];
		} else {
		    $mimetype = 'image/jpeg'; 
		}
	}
 
	if (!is_null($cover)) { 
		header("Content-Type: " . $mimetype); 
		if (isset($getID3->info['id3v2']['APIC'][0]['image_bytes'])) { 
			header("Content-Length: " . $getID3->info['id3v2']['APIC'][0]['image_bytes']); 
		} 
		echo($cover); 
	}
}
 
function isMusicFile($filename) {
	$tmp = pathinfo($filename);
	if( $tmp['extension'] == 'm4a' || $tmp['extension'] == 'mp3' ) return true;
	else return false;
}
?>

의견을 남겨주세요

Trackback URL : http://blueruin.org/archives/599/trackback

WP Theme & Icons by N.Design Studio
Entries RSS Log in