<?php
/*
 *  textorizer - a PHP wrapper for Max Froumentin's Textorizer :-
 *
 *      http://www.w3.org/People/maxf/textorizer/ 
 *
 *  written by Paul Downey (paul.downey@whatfettle.com)
 *
 *  http://textorizer.whatfettle.com
 *
 *  released under the W3C® SOFTWARE NOTICE AND LICENSE
 *  http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
 *
 *  $Header$
 *
 */

/*
 *  maximum file length in Kbytes
 */
$maxsize 128;

/*
 *  finding a graceful way to limit file 
 *  read from a http stream tricky in php4
 */
function get_contents($filename$maxsize
{
    
$ch curl_init();
    
curl_setopt ($chCURLOPT_URL$filename);
    
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
    
curl_setopt ($chCURLOPT_CONNECTTIMEOUT5);
    
$contents curl_exec($ch);
    
curl_close($ch);
    return 
$contents;
}


/*
 *  get parameter
 */
function get_value($name$min$max$default)
{
    
$value $_GET[$name];
    
trim($value);

    if (
$value == ""
        return 
$default;
    if (
$value $min)
        return 
$min;
    if (
$value $max
        return 
$max;

    return 
$value;
}

    
$form $_GET['form'];
    
$image $_GET['image'];
    
$text $_GET['text'];
    
$nstrokes get_value('nstrokes'12000350);
    
$threshold get_value('threshold'12000100);
    
$width get_value('width'110000800);
    
$height get_value('height'110000600);


    if (
"" != $image && "" != $text && "" == $form)
    {
        
/*
         *  fetch image
         */
        
$img imagecreatefromstring(get_contents($image$maxsize));

        
/*
         *  convert image into png blob 
         */
        
ob_start();
        
imagepng($img);
        
$blob ob_get_contents();
        
ob_end_clean();
        unset(
$img);

        
$text utf8_encode($text);

        
/*
         *  textorize the png as SVG
         */
        
header("Content-type: image/svg+xml;charset=UTF-8");
        
dl("textorize.so");
        
textorize($blob$text$nstrokes$threshold$width$height);
        exit (
0);
    }


/*
 *  an example image to get people going ..
 */
if ($image == "")
{
        
$image "http://www.flickr.com/images/flickr_logo_beta.gif";
}
if (
$text == "")
{
        
$text "flickr\ntextorized";
}


print <<<EOF

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Textorizer - vectorise a picture using text strings</title>
    <link rel="stylesheet" type="text/css" href="style.css" >
    <link rel="shortcut icon" href="favicon.ico" >
    <link rel="icon" href="favicon.ico" >
</head>
<body>
<p>
<a href="http://textorizer.whatfettle.com/" style="text-decoration: none;"><span class="title">textorizer</span></a>
    <span class="byline">- <a href="http://www.w3.org/People/maxf/textorizer/">textorize</a> a picture</span>
</p>

<form action="" method="get">
  <p>
    <br/> image:
    <br/> <input name="image" type="text" size="50" value="$image"/>
    <br/> <i>jpeg, png, gif, or xpm file less than 
${maxsize}K in length.</i>
    <br/>

    <br/> text:
    <br/> <textarea name="text" rows="3" cols="15">$text</textarea>
    <br/> <i>each line of text is used in turn.</i>
    <br/>

    <br/> strokes:
    <br/> <input name="nstrokes" type="text" size="5" value="$nstrokes"/>
    <br/> <i>number of lines of text to write.</i>
    <br/>

    <br/> threshold:
    <br/> <input name="threshold" type="text" size="5" value="$threshold"/>
    <br/> <i>controls the edge-detection.</i>
    <br/>

    <br/> width:
    <br/> <input name="width" type="text" size="5" value="$width"/>
    <br/> <i>output <a href="http://www.w3.org/Graphics/SVG/">SVG</a> width in pixels.</i>
    <br/>

    <br/> height:
    <br/> <input name="height" type="text" size="5" value="$height"/>
    <br/> <i>output <a href="http://www.w3.org/Graphics/SVG/">SVG</a> height in pixels.</i>
    <br/>


    <br/>
    <br/>
    <input type="submit" value="Textorize!"/>
  </p>
</form>

<div id='nav'>
<p>

<a href='about'>About</a>

</p>
</div>

</body>
</html>

EOF

?>