Get album art from MP3 PHP - php

I am using this code to get ID3 information of an MP3 File.
<?php
class CMP3File {
var $title;var $artist;var $album;var $year;var $comment;var $genre;
function getid3 ($file) {
if (file_exists($file)) {
$id_start=filesize($file)-128;
$fp=fopen($file,"r");
fseek($fp,$id_start);
$tag=fread($fp,3);
if ($tag == "TAG") {
$this->title=fread($fp,30);
$this->artist=fread($fp,30);
$this->album=fread($fp,30);
$this->year=fread($fp,4);
$this->comment=fread($fp,30);
$this->genre=fread($fp,1);
fclose($fp);
return true;
} else {
fclose($fp);
return false;
}
} else { return false; }
}
}
?>
I am then using this code to call it.
include ("CMP3File.php");
$filename="music_file.mp3";
$mp3file=new CMP3File;
$mp3file->getid3($filename);
echo "Title: $mp3file->title<br>\n";
echo "Artist: $mp3file->artist<br>\n";
echo "Album: $mp3file->album<br>\n";
echo "Year: $mp3file->year<br>\n";
echo "Comment: $mp3file->comment<br>\n";
echo "Genre: " . Ord($mp3file->genre) . "<br>\n";
Is there a way to also get and echo out the album art as an image?

Related

Can file_get_content be used along with preg_replace?

How can I get the file content from preg_replace?
<?php
function get($it) {
$r = array("~<script src='(.*?)'></script>~");
$w = array("<script type='text/javascript'>' . file_get_contents($1) . '</script>");
$it = preg_replace($r, $w, $it);
return $it;
}
$it = "<script src='/script.js'></script>";
echo get($it);
?>
It returns <script type='text/javascript'>' . file_get_contents(/script.js) . '</script>
If the path is relative as in your example the file_get_contents won't work but this should get you closer:
function get($it) {
return preg_replace_callback("~<script src='(.*?)'></script>~", function($match){
return "<script type='text/javascript'>" . file_get_contents($match[1]) . '</script>';
}, $it);
}
$it = "<script src='/script.js'></script>";
echo get($it);

Add some data from php function into mpdf

I have php function, whose create some blocks with data from loop.
finction.php:
function drawcomplect() {
for ($i=0;$i<count($new_arr);$i++) {
if ($i%2==0) {
echo "<div class='numeric'>".$j++."</div>";
echo "<div class='item'>".$new_arr[$i]."</div>";
} else {
echo "<div class='count'>".$new_arr[$i]."</div>";
}
}
}
And mPdf file for output:
$html ='<html>'.drawcomplect().'</html>'
If run this function in php file, it's work. But mPdf not work, print "Page error". How I can add data to pdf?
function drawcomplect() {
$str = '';
for ($i=0;$i<count($new_arr);$i++) {
if ($i%2==0) {
$str .="<div class='numeric'>".$j++."</div><div class='item'>".$new_arr[$i]."</div>";
} else {
$str .="<div class='count'>".$new_arr[$i]."</div>";
}
}
return $str;
}
ANd Mpdf:
$html ='<html><body>'.drawcomplect().'</body></html>'

removing multiple files from a web-directory using foreach() function

I want to delete multiple files from my directory, and for this I am using the following code
$x=array(".index.php",".code.html","about.txt");
foreach($x as $a)
unlink($a);
The wired thing with this code is that it sometime works and sometimes doesn't, and no errors.
Is there anything that I am missing?
Add some monitoring to your code, to see what happens:
foreach($x as $a) {
echo "File $a ";
if (file_exists($a)) {
if (is_file($a)) {
echo "is a regular file ";
} else {
echo "is not a regular file ";
}
if (is_link($a)) {
echo "is a symbolic link ";
}
if (is_readable($a)) {
echo " readable";
} else {
echo " NOT readable";
}
if (is_writeable($a)) {
echo " and writeable ";
} else {
echo " and NOT writeable ";
}
echo "owned by ";
echo posix_getpwuid(fileowner($a)) ['name'];
if (unlink($a)) {
echo "- was removed<br />\n";
} else {
echo "- was NOT removed<br />\n";
}
} else {
echo "doesn't exist<br />\n";
}
}
Also read this comment on the PHP manual page about unlinking files.
If you have to use a path for your file, convert it to a real path with the function realpath() - see https://php.net/manual/en/function.realpath.php

Facebook Graph API : Parsing the output

I am trying to get the names of my friends using FB Graph API with this call :
$friends = file_get_contents('https://graph.facebook.com/me/friendsaccess_token='.$session["access_token"]);
echo "Friends : $friends\n";
This gives me a list of the form :
{"data":[{"name":"ABC XYZ","id":"12212839"},{"name":"PQR GHI","id":"5004678"}]}
I want to be able to store only the NAMES in an array. How do I use $friends to get the names ? Something like $friends['name'] doesn't seem to work.
Please help.
Thank you.
$friends = json_decode($friends);
foreach($friends['data'] as $friend)
{
echo $friend['name'];
}
The return is a json object, you need to decode it. Although I strongly urge you to use an SDK such as http://github.com/facebook/php-sdk/
If this doesn't work try:
$friends = json_decode($friends);
foreach($friends->data as $friend)
{
echo $friend->name;
}
Here is what I did to get the posts information.. crude but it works. Note that the shares likes comments and reactions are one level deeper in the JSON object
$posts = json_decode($output); // from FB Graph v2.8 API call
foreach($posts->data as $post)
{
echo "MESSAGE: ", $post->message, "<br>";
echo "NAME: ", $post->name, "<br>";
echo "TYPE: ", $post->type, "<br>";
echo "ID: ", $post->id, "<br>";
echo "LINK: ", $post->link, "<br>";
echo "PERMALINK: ", $post->permalink_url, "<br>";
echo "CREATED: ", $post->created_time, "<br>";
if($post->shares->count == "") { $shares = "0"; } else { $shares = $post->shares->count; }
echo "SHARES: ", $shares, "<br>";
if($post->reactions->summary->total_count == "") { $reactions = "0"; } else { $reactions = $post->reactions->summary->total_count; }
echo "REACTIONS: ", $reactions, "<br>";
if($post->comments->summary->total_count == "") { $comments = "0"; } else { $comments = $post->comments->summary->total_count; }
echo "COMMENTS: ", $comments, "<br>";
if($post->likes->summary->total_count == "") { $likes = "0"; } else { $likes = $post->likes->summary->total_count; }
echo "LIKES: ", $likes, "<br>";
echo "<br><br>";
}

How to hide or display text and tags with PHP?

I was wondering how would hide the text Location if the city state and country were not present and how would I let it display if any of the variables were present?
<p>Location:
<?php
if (!empty($city))
{
echo $city .',';
}
if (!empty($state))
{
echo $state;
}
if (!empty($country))
{
echo ' ' . $country;
}
?>
</p>
Use:
<?php
$address = "";
if (!empty($city))
{
$address_parts[] = $city;
}
if (!empty($state))
{
$address_parts[] = $state;
}
if (!empty($country))
{
$address_parts[] = $country;
}
$address = implode( ", ", $address_parts);
if (!empty($address))
{
echo "<p>Location: " . $address . "</p>";
}
?>
Build the address up and then test if the address is still empty before displaying location.
Edit
Changed how the address is built up. Rather than concatenate everything, the address parts are added to an array and then imploded with the appropriate separator.
Wrap it in a PHP block as well:
<?php
// Check if any of them is not empty.
if(!empty($city) || !empty($state) || !empty($country)){
echo "<p>Location:";
if (!empty($city))
{
echo $city .',';
}
if (!empty($state))
{
echo $state;
}
if (!empty($country))
{
echo ' ' . $country;
}
echo "</p>";
}
in not exactly same but a similar problem, i used this method.
may be this helps you too:)
<?php
function publish($buffer){
global $location;
if (empty($location))
$buffer="";
return $buffer;
}
ob_start("publish");
$location=$_GET["location"];
echo "Location: $location";
ob_end_flush();
?>
test the url with location parameter and without a parameter.
And you can find more detail about this in php.net output control functions

Categories