Micolithe
Agender
36 years old
Philadelphia, PA
Online Now
Last Login: 08/30/2007

Agender Enby, Trans, Gay, AND the bearer of the gamer's curse. Not a man, not a woman, but instead I am puppy.
I got a fat ass and big ears.

--

Yes I did the cooking mama Let's Play way back when. I post alot about Tech (mostly how it sucks) and Cooking and Music and Television Shows and the occasional Let's Play video
💖@FadeToZac

--

We all do what we can ♫

So we can do just one more thing ♫

We can all be free ♫

Maybe not in words ♫

Maybe not with a look ♫

But with your mind ♫


last.fm listening



<?php


		function splitlines($input_string, $character_limit){
                $arr_words=explode(' ',$input_string);
                //echo($input_string);
                $current_length=0;
                $line1='';
                $line2='';
                foreach($arr_words as $word){
                                $len=strlen($word);
                                $current_length=$current_length+$len;
                                if($current_length<=$character_limit){
                                        //echo($word);
                                        $line1=$line1.$word.' ';
                                        //echo('<br>');
                                        //echo($line1);
                                }else{
                                        //echo($word);
                                        $line2=$line2.$word.' ';
                                        //echo('<br>');
                                        //echo($line2);
                                }
                }
                //echo($line1);
                //echo('<br>');
                //echo($line2);
                return array($line1,'  '.$line2);
        }
		
	//assert_options(ASSERT_ACTIVE, 1);
	//assert_options(ASSERT_BAIL, 1);
	//assert_options(ASSERT_QUIET_EVAL, 1);
	//error_reporting(E_ALL);
	//ini_set('display_errors', 1);
	//I used these pretty heavily in testing - if firefox tells you "this image contains errors" you may want to comment 
	//out the header content type png line below & uncomment the lines above to see what's actually going on. php is
	//notoriously difficult to debug.
	
	$last_fm_user_id='removed';
	$social_media_str='cohost.org/micolithe';
	$last_fm_api_key='removed';
	//You can get a last fm API key by going to https://www.last.fm/api/account/create
	
	
	//I fucking hate PHP so much oh my god
    $url='https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='.$last_fm_user_id.'&api_key='.$last_fm_api_key.'&limit=1';
	$x=simplexml_load_file($url); //the way last fm returned the JSON didnt play nicely with PHP. XML was a little better, weirdly enough.
	if ($x===false){
		echo("There has been porblem");
	}
	$artist=$x->recenttracks->track[0]->artist;
	$album=$x->recenttracks->track[0]->album;
	$track=$x->recenttracks->track[0]->name;
	$art=$x->recenttracks->track[0]->image[1]; 
	//Album art returns four sizes for album art. 0 - small 34x34, 1 = medium 64x64, 2 = large 174x174,  3 = extralarge 300x300.
	//I should probably search for the tag with the attribute size=medium to make this safer but it seems like it returns them in a consistent order
	//This is a problem for future micolithe
	$unix_ts=0;
	$formatted_date="???";
	//currently playing tracks do not have a date key in the API.
	$lastfm_art_format=substr($art,-4);
	if (array_key_exists('date',get_object_vars($x->recenttracks->track[0]))){
		$unix_ts=$x->recenttracks->track[0]->date->attributes()->uts;
		$formatted_date=date('Y-m-d g:i:s a',intval($unix_ts));  
		//if there is a date parse it
	}else{
		$formatted_date="Playing right now!"; 
		//if there isn't just say now
	}
	if ($lastfm_art_format=='jpeg' || $lastfm_art_format=='.jpg'){ 
	//images can be png or jpg, which have different image creation functions
		$albumimage=imagecreatefromjpeg($art);
	}
	elseif($lastfm_art_format=='.png'){
		$albumimage=imagecreatefrompng($art);
	}
	else{
		$albumimage=imagecreatefrompng('missing_album_art.png');  
		//i don't think this would ever happen since last fm seems to return placeholder art, but just in case
                //actually this happens all the time, the placeholder art is only
                //for the currently playing track. if you stop listening it stops
                //giving you a placeholder image.
	}
	header("Content-type: image/png");  //force this php file to behave like an image :)
	$img=imagecreatefrompng('last_fm_bg.png');
	$blacktext=imagecolorallocate($img,0,0,0);
	imagealphablending($img,true);
	//$albumimage=imagecreatefromjpeg($art);
	imagesavealpha($img,true);
	imagecopymerge($img,$albumimage,16,55,0,0,64,64,100);
	$arr_artist=splitlines($artist,25);
	$arr_album=splitlines($album,25);
	$arr_track=splitlines($track,25);
        //arial.ttf is sitting in the same directory with the base image & php script
	imagettftext($img,10,0,90,50,$blacktext,'./arial.ttf',$arr_artist[0]);  //ok the order of imagettftext's args is a little confusing
	imagettftext($img,10,0,90,65,$blacktext,'./arial.ttf',$arr_artist[1]);  
	imagettftext($img,10,0,90,80,$blacktext,'./arial.ttf',$arr_album[0]);
	imagettftext($img,10,0,90,95,$blacktext,'./arial.ttf',$arr_album[1]);	//the order is:
	imagettftext($img,10,0,90,110,$blacktext,'./arial.ttf',$arr_track[0]);
	imagettftext($img,10,0,90,125,$blacktext,'./arial.ttf',$arr_track[1]);	//image, font size, rotation angle, x position (from left), y position (from top), text color, true type font, string
	imagettftext($img,8,0,90,140,$blacktext,'./arial.ttf',$formatted_date);
	imagettftext($img,6,0,10,160,$blacktext,'./arial.ttf',$social_media_str."  |  last.fm/user/".$last_fm_user_id);
	imagepng($img);  //slambo
	imagedestroy($img); //wangjangle
?>
syntax highlighting by codehost

You must log in to comment.