<?php
// Define the image type header
header('Content-Type: image/png');
// Set font by inserting the neame of the fonfile between the quotes in the realpath() function call below.
// Note: the font will need to be in the same directory as this php script.
// Capitalization must match that of the fontfile name as well.
// I preferred the appearance of Courier Bold but the choice here is all yours.
// You can upload and use and TrueType font you want. You will only need to change the fontfile name below.
$font = realpath("cour.ttf");
// Open and read the contents of the Audiogalaxy text file
$handle = fopen("http://www.audiomatch.net/text/mkt.txt", "rb");
$contents = "";
do {
$data = fread($handle, 8192);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while (true);
fclose($handle);
// The purpose of this if/else statement is to allow you to put in your own message for when you are not listening.
if($contents){
$text = $contents;
}
else{
$text = "Absolutely Nothing";
}
$fontsize = 10;
// Get text image bounding box and set height and width for both lines of the image
$atextcords = imagettfbbox ($fontsize, 0, $font, "Currently Listening to:");
$atextwidth = $atextcords[2] + 2;
$atextheight = $atextcords[7] - ($atextcords[7] * 2) + 7;
$btextcords = imagettfbbox ($fontsize, 0, $font, $text);
$btextwidth = $btextcords[2] + 2;
$btextheight = $btextcords[7] - ($btextcords[7] * 2) + 7;
// Calculate image height and width based on bounding box values of both lines of text
if($atextwidth > $btextwidth){
$imgwidth = $atextwidth * 1.2;
}
else{
$imgwidth = $btextwidth * 1.2;
}
$imgheight = ($atextheight + $btextheight) * 1.5;
// Create image and allocate image colors
$im = imagecreate ($imgwidth, $imgheight);
$black = ImageColorAllocate ($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);
// Print text in image with initial x/y positions set relative to imgsize and text height and textwidth
ImageTTFText ($im, $fontsize, 0, ($imgwidth-$atextwidth)/2, ($imgheight-$atextheight)/2, $white, $font,"Currently Listening to:");
ImageTTFText ($im, $fontsize, 0, ($imgwidth-$btextwidth)/2, ($imgheight-($atextheight+$btextheight)/1.8), $white, $font,$text);
// Output and destroy image
Imagepng ($im);
ImageDestroy ($im);
?>