如何在PHP中使用此类数据制作图像?

时间:2018-10-13 21:15:30

标签: php html

我正在使用getID3()函数(可在 https://github.com/JamesHeinrich/getID3)的图像封面 MP3文件。

我正在使用这部分代码:

$path="mp3/3.mp3";
require_once('getid3/getid3.php');
$getID3 = new getID3;
$getID3->setOption(array('encoding'=>$TextEncoding)); 
$ThisFileInfo = $getID3->analyze($path);
getid3_lib::CopyTagsToComments($ThisFileInfo);  

$info=$ThisFileInfo['comments'];
$TextEncoding = 'UTF-8';

$data=$info['picture'][0]['data'];
$mime_type=$info['picture'][0]['image_mime'];
$im_width=$info['picture'][0]['image_width'];
$im_height=$info['picture'][0]['image_height']; 

为了显示图像,我用这个:

echo'<html><body>'."\n".'<tr><td><img src="data:'.$mime_type.';base64,'.base64_encode($data).'" width="'.$im_width.'" height="'.$im_height.'"></td></tr></body></html>'."\n";

但是什么也没显示。

我想将$data另存为图像文件。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

要简单地从字符串或任何格式创建图像文件,您需要使用gd扩展名的php。

  

从字符串

$toFilePath = '/path/to/save/data/filename';  
//notice the missing file extension.It will be added according to the mime_type.
//Make sure you have the write rights to the folder/file above

$im = imagecreatefromstring($data);  //create image data from the string

if ($im !== false) {  //if the image creation is successful
    switch($mime_type) {
        case 'image/jpeg':
        case 'image/jpg':
             imagejpg($im, $toFilePath.'.jpg', 100);
             break;
        case 'image/png':
             imagepng($im, $toFilePath . '.png');
             break;
        case 'image/gif':
             imagegif($im, $toFilePath . '.gif');
             break;
        case 'image/bmp':
             imagebmp($im, $toFilePath . '.bmp');
             break;
    }
    imagedestroy($im);
}
  

从BLOB / BINARY数据下面的代码段是专门为此库定制的。

$toFilePath = '/path/to/save/data/filename';  
//notice the missing file extension.It will be added according to the mime_type.
//Make sure you have the write rights to the folder/file above

switch($mime_type) {
    case 'image/jpeg':
    case 'image/jpg':
        $toFilePath .= '.jpg';
        break;
    case 'image/png':
        $toFilePath .=  '.png';
        break;
    case 'image/gif':
        $toFilePath .=  '.gif';
        break;
    case 'image/bmp':
        $toFilePath .=  '.bmp';
        break;
}
if ($handle = fopen($toFilePath, 'wb')) {
    fwrite($handle, $data);
    fclose($handle);
}

该代码取自PyCharm,并进行了精简以符合OP的需求

  

从文件中提取所有图像

function extractImages($pictureInfo) {
    $toFilePath = __DIR__ . DIRECTORY_SEPARATOR . 'output' . DIRECTORY_SEPARATOR . 'filename';
    //notice the missing file extension.It will be added according to the mime_type.
    //Make sure you have the write rights to the folder/file above

    for ($i = 0, $count = count($pictureInfo);$i < $count;$i++) {

        $data = $pictureInfo[$i]['data'];
        $mime_type = $pictureInfo[$i]['image_mime'];

        switch ($mime_type) {
            case 'image/jpeg':
            case 'image/jpg':
                $toFilePath .= '_' . $i . '.jpg';
                break;
            case 'image/png':
                $toFilePath .= '_' . $i . '.png';
                break;
            case 'image/gif':
                $toFilePath .= '_' . $i . '.gif';
                break;
            case 'image/bmp':
                $toFilePath .= '_' . $i . '.bmp';
                break;
        }
        if ($handle = fopen($toFilePath, 'wb')) {
            fwrite($handle, $data);
            fclose($handle);
        }
    }
}

$path = __DIR__ . DIRECTORY_SEPARATOR . "mp3" . DIRECTORY_SEPARATOR . "3.mp3";
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'getid3' . DIRECTORY_SEPARATOR . 'getid3.php');
$TextEncoding = 'UTF-8';
$getID3 = new getID3();
$getID3->setOption(array('encoding' => $TextEncoding));
$ThisFileInfo = $getID3->analyze($path);

getid3_lib::CopyTagsToComments($ThisFileInfo);

$info = $ThisFileInfo['comments'];

if (isset($info['picture'])) extractImages($info['picture']);
else {
    echo 'no picture tag';
}