如何用PHP结合getID3实现通过img标签返回MP3歌曲封面图?
Complete PHP Solution to Return MP3 Cover Art as Image
Here's a polished, secure version of the script that fulfills your requirement. It includes input sanitization, error handling, and proper response formatting for image requests:
<?php // Sanitize input to prevent path traversal attacks $song = isset($_GET['song']) ? basename($_GET['song']) : ''; if (empty($song)) { // Return a placeholder or 404 if no song name is provided header('HTTP/1.1 400 Bad Request'); exit; } // Define your MP3 storage directory (adjust this to your actual path) $mp3Directory = './mp3_files/'; $mp3Path = $mp3Directory . $song . '.mp3'; // Check if the MP3 file exists if (!file_exists($mp3Path) || !is_readable($mp3Path)) { // Return a default placeholder image if the file doesn't exist header('Content-Type: image/png'); // You can replace this with your own placeholder image data or redirect to a static placeholder echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH5gMVESkqQzkWSwAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAUSURBVBgZBcHPcsAABAAAQ+zWF2AAAAAElFTkSuQmCC'); exit; } // Include getID3 library (adjust path to match your installation) require_once("getID3-1.9.15/getid3/getid3.php"); $getID3 = new getID3; $fileInfo = $getID3->analyze($mp3Path); // Check if cover art exists in the MP3 metadata if (isset($fileInfo['comments']['picture'][0])) { $imageData = $fileInfo['comments']['picture'][0]['data']; $imageMimeType = $fileInfo['comments']['picture'][0]['image_mime']; // Send proper image headers header('Content-Type: ' . $imageMimeType); header('Content-Length: ' . strlen($imageData)); // Prevent caching if you want fresh covers every time (optional) // header('Cache-Control: no-cache, no-store, must-revalidate'); echo $imageData; } else { // Return default placeholder if no cover art is found header('Content-Type: image/png'); echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH5gMVESkqQzkWSwAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAUSURBVBgZBcHPcsAABAAAQ+zWF2AAAAAElFTkSuQmCC'); } exit; ?>
Key Improvements & Notes:
- Input Sanitization: Uses
basename()to block path traversal attacks, ensuring users can't access files outside your MP3 directory. - File Validation: Checks if the MP3 exists and is readable before processing.
- Error Handling: Returns appropriate HTTP responses or a placeholder image for missing files/cover art.
- Secure Directory: Defines a specific MP3 storage directory to limit access to only intended files.
- Proper Headers: Sends correct MIME type and content length headers so browsers recognize the response as an image.
Usage Example:
Just place this script as imageReturn.php in your web root, adjust the $mp3Directory path to your MP3 folder, and use it in HTML like:
<img src="/imageReturn.php?song=MyFavoriteSong" alt="Song Cover">
内容的提问来源于stack exchange,提问作者Nick
相关产品推荐
相关产品推荐

