<?php
/*
 * Turning String Image Generator
 *
 * Copyright (c) 2004 Colin Viebrock <colin@viebrock.ca>
 * 
 * Usage of the works is permitted provided that this
 * instrument is retained with the works, so that any entity
 * that uses the works is notified of this instrument.
 * 
 * DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
 */

session_start();


/* define the characters that can make up the Turing string
 *
 * [20-Apr-2004] mixed-case is apparently a bit of a problem,
 * so lets pick one or the other
 */

$src  '';
$src .= 'ABCDEFGHJKLMNPQRSTUVWXYZ';        /* no I, O */
#$src .= 'abcdefghijkmnpqrstuvwxyz';        /* no l, o */
$src .= '23456789';                        /* no 1, 0 */

if (mt_rand(0,1)==0) {
    
$src strtoupper($src);
}

$srclen strlen($src)-1;


/* how long is the Turing string */

$length 8;


/* what font file to use */

$font 'bboron.ttf';


/* output type */

# $output_type='jpeg';
$output_type='png';


/* font size range, angle range, character padding */

$min_font_size 12;
$max_font_size 20;
$min_angle = -15;
$max_angle 15;
$char_padding 2;


/* initialize variables */

$_SESSION['turing_string'] = '';
$data = array();
$image_width $image_height 0;


/* build the data array of the characters, size, placement, etc. */

for($i=0$i<$length$i++) {

    
$char substr($srcmt_rand(0,$srclen), 1);
    
$_SESSION['turing_string'] .= $char;

    
$size mt_rand($min_font_size$max_font_size);
    
$angle mt_rand($min_angle$max_angle);

    
$bbox ImageTTFBBox$size$angle$font$char );

    
$char_width max($bbox[2],$bbox[4]) - min($bbox[0],$bbox[6]);
    
$char_height max($bbox[1],$bbox[3]) - min($bbox[7],$bbox[5]);

    
$image_width += $char_width $char_padding;
    
$image_height max($image_height$char_height);

    
$data[] = array(
        
'char'        => $char,
        
'size'        => $size,
        
'angle'        => $angle,
        
'height'    => $char_height,
        
'width'        => $char_width,
    );
}


/* calculate the final image size, adding some padding */

$x_padding 12;
$image_width += ($x_padding 2);
$image_height = ($image_height 1.5) + 2;


/* build the image, and allocte the colors */

$im ImageCreate($image_width$image_height);
$r 51 mt_rand(4,5);
$g 51 mt_rand(4,5);
$b 51 mt_rand(4,5);
$color_bg        ImageColorAllocate($im,  $r,  $g,  $b );

$r 51 mt_rand(3,4);
$g 51 mt_rand(3,4);
$b 51 mt_rand(3,4);
$color_line0    ImageColorAllocate($im,  $r,  $g,  $b );

$r 51 mt_rand(3,4);
$g 51 mt_rand(3,4);
$b 51 mt_rand(3,4);
$color_line1    ImageColorAllocate($im,  $r,  $g,  $b );

$r 51 mt_rand(1,2);
$g 51 mt_rand(1,2);
$b 51 mt_rand(1,2);
$color_text        ImageColorAllocate($im,  $r,  $g,  $b );

$color_border    ImageColorAllocate($im,   0,   0,   );


/* make the random background lines */

for($l=0$l<10$l++) {

    
$c 'color_line' . ($l%2);

    
$lx mt_rand(0,$image_width+$image_height);
    
$lw mt_rand(0,3);
    if (
$lx $image_width) {
        
$lx -= $image_width;
        
ImageFilledRectangle($im0$lx$image_width-1$lx+$lw, $$c );
    } else {
        
ImageFilledRectangle($im$lx0$lx+$lw$image_height-1, $$c );
    }

}




/* output each character */

$pos_x $x_padding + ($char_padding 2);
foreach(
$data as $d) {

    
$pos_y = ( ( $image_height $d['height'] ) / );
    
ImageTTFText($im$d['size'], $d['angle'], $pos_x$pos_y$color_text$font$d['char'] );

    
$pos_x += $d['width'] + $char_padding;

}


/* a nice border */

ImageRectangle($im00$image_width-1$image_height-1$color_border);



/* display it */


switch ($output_type) {
  case 
'jpeg':
    
Header('Content-type: image/jpeg');
    
ImageJPEG($im,NULL,100);
    break;

  case 
'png':
  default:
    
Header('Content-type: image/png');
    
ImagePNG($im);
    break;

ImageDestroy($im);