<?php

    
# PHP script to render iTunes currently playing track text as bitmap
    #
    # Needs to be used together with an AppleScript which can be downloaded at
    #
    #     http://www.entropy.ch/about/query-itunes-loop.applescript.sit
    #

    
$ttfont             = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaSansDemiOblique.ttf";
    
#$ttfont             = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaBrightItalic.ttf";
    #$ttfont             = "/Library/WebServer/www.entropy.ch/about/msfonts-1.2.1/main/georgiai.ttf";
    #$ttfont             = "/Library/WebServer/www.entropy.ch/about/msfonts-1.2.1/main/ariali.ttf";
    
$ttsize             = 9;

    
# We break lines after a line of text reaches this width
    #
    
$image_target_width = 430;

    
$itunes_status_raw = `cat /tmp/itunes-status.txt`;

    
# Mac to ISO translation
    #
    # (no longer needed, now using UTF-8)
#    $itunes_status = strtr(
#        $itunes_status_raw,
#        "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa1\xa4\xa6\xa7\xa8\xab\xac\xae\xaf\xb4\xbb\xbc\xbe\xbf\xc0\xc1\xc2\xc7\xc8\xca\xcb\xcc\xd6\xd8\xdb\xe1\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf1\xf2\xf3\xf4\xf8\xfc",
#        "\xc4\xc5\xc7\xc9\xd1\xd6\xdc\xe1\xe0\xe2\xe4\xe3\xe5\xe7\xe9\xe8\xea\xeb\xed\xec\xee\xef\xf1\xf3\xf2\xf4\xf6\xf5\xfa\xf9\xfb\xfc\xb0\xa7\xb6\xdf\xae\xb4\xa8\xc6\xd8\xa5\xaa\xba\xe6\xf8\xbf\xa1\xac\xab\xbb\xa0\xc0\xc3\xf7\xff\xa4\xb7\xc2\xca\xc1\xcb\xc8\xcd\xce\xcf\xcc\xd3\xd4\xd2\xda\xdb\xd9\xaf\xb8"
#    );

    
$itunes_status = $itunes_status_raw;

    
# Break text into lines at whitespace boundaries
    #
    
$matches = array();
    
#preg_match_all('/("[^"]+",?|\S+)/', $itunes_status, $matches);
    
preg_match_all('/(\S+)/', $itunes_status, $matches);
    
$matches = $matches[1];

    
$output = array();
    
$current_line = array();

    while (
count($matches) > 0) {

        
array_push($current_line, array_shift($matches));

        
$current_text = join("", $current_line);
        
$current_ttbbox = imagettfbbox($ttsize, 0, $ttfont, $current_text);
        
$current_ttwidth = $current_ttbbox[2] - $current_ttbbox[0];

        if (
count($matches) > 0) {

            if (
$current_ttwidth > $image_target_width) {

                
# No more room, break line
                #
                
$output = array_merge($output, $current_line);
                
array_push($output, "\r\n ");
                
$current_line = array();
            } else {

                
# More room on this line, just add a space
                #
                
array_push($current_line, " ");
            }

        }

    }    

    
# Collect lines into $output
    #
    
$output = array_merge($output, $current_line);
    
$itunes_status = join("", $output);


    
# Calculate the bounding box of the given string with
    # the above font parameters
    #
    
$ttbbox = imagettfbbox($ttsize, 0, $ttfont, $itunes_status);

    
# array index 2 contains lower right X, 0 contains lower left X,
    # difference is our text width
    #
    
$ttwidth = $ttbbox[2] - $ttbbox[0];
    
$imgwidth = $ttwidth + 10;

    
$imgheight = ($ttbbox[1] - $ttbbox[7]) + 8;

    if (
$imgwidth < 500) {
        
$imgwidth = 500;
    }

    
$im = @ImageCreate($imgwidth, $imgheight) or die ("Cannot Initialize new GD image stream");
    
$transparent_color   = ImageColorAllocateAlpha($im, 103, 104, 140, 70);

    
$text_color = ImageColorAllocate($im, 255, 255, 255);
    
$text_color_border = ImageColorAllocate($im, 200, 200, 200);
    
$text_color_shadow = ImageColorAllocate($im, 70, 70, 70);
    
$text_color_black = ImageColorAllocate($im, 0, 0, 0);

    
ImageRectangle($im, 0, 0, $imgwidth - 1, $imgheight - 1, $text_color_shadow);

    
$pos_x = 4;
    
$pos_y = $ttsize + 5;
    
$linespacing = 1.2;

    
imagefttext($im, $ttsize, 0, $pos_x + 1, $pos_y + 1, $text_color_shadow, $ttfont, $itunes_status, array("linespacing" => $linespacing));
    
imagefttext($im, $ttsize, 0, $pos_x,     $pos_y,     $text_color, $ttfont, $itunes_status, array("linespacing" => $linespacing));

    
header("Content-type: image/png");
    
ImagePng($im);

    
ImageDestroy($im);

?>