Peter Mackenzie's Random Parallelogram And Circle Picture Generator

Picture Size:
Object Size:
Circle Probability:

<?php    // graphics php
    //this is the file that makes that graphic

    //This makes an image to certain paremeters with randomly generated shapes
    
function rectangle($centerx, $centery, $xlength, $ylength, $xskew = 0, $yskew = 0)
        {
        
//this will return an array with 4 cordinates for a rectangle of a given center point
        //slope, and length of sides
        //slope. 1/slope = how much y per 1 x.
        
$tempshape = array(0,0, 0,0, 0,0, 0,0);//upper left, upper right, lower right, lower left
        //if there is no slope
        
$halfx = intval($xlength / 2);
        
$halfy = intval($ylength / 2);

        
$tempshape = array((-1 * $halfx) + $centerx +$xskew, $halfy + $centery + $yskew, $halfx + $centerx + $xskew, $halfy + $centery, $halfx + $centerx, (-1 * $halfy) + $centery,(-1 * $halfx) + $centerx, (-1 * $halfy) + $centery + $yskew);


        return 
$tempshape;

        }


    
// create a blank image that is 400 by 300 pixels in size
    
$picwidth = isset($_GET['picwidth']) ? $_GET['picwidth'] : 1000;
    
$picheight = isset($_GET['picheight']) ? $_GET['picheight'] : 1000;
    
$circleprob = isset($_GET['circleprob']) ? $_GET['circleprob'] : 25;
    
$objectsize = isset($_GET['objectsize']) ? $_GET['objectsize'] : .25;
    
//setup a check for maxpercent size of objects as well
    //also, lets get the max and min number of objects set

    //$circleprob = 100;//set this 0 to 100, higher the number, the more likely cirlces are
    //$picwidth = 1000;
    //$picheight = 1000;
    
$image = imagecreate($picwidth, $picheight);

    
//set background color random
    
$na = imagecolorallocate($image, rand(0,255), rand(0,255), rand(0,255));    // var $na is not used

    // set the current foreground color to RGB (0, 0, 255) which is BLUE
    //$color = imagecolorallocate ($image, 0, 0, 255);

    
$numberofthings = rand (100,250);
    
$maxwidth = intval($picwidth * $objectsize);
    
$maxheight = intval($picheight * $objectsize);

    for (
$x = 0;$x < $numberofthings;$x++)
        {
        
// populate the polygon (a triangle) with paired x-y coordinates


        
$color = imagecolorallocate ($image, rand(0,250), rand(0,250), rand(0,250));
        if (
rand(1,100) > $circleprob)
            {
            
$centerx = rand ($maxwidth - 1, $picwidth - 1 - $maxwidth);
            
$centery = rand ($maxheight - 1, $picheight - 1 - $maxheight);
            
$rectanglearray = rectangle($centerx, $centery, rand(1, $maxwidth), rand(1, $maxheight), rand(-1 * $maxwidth, $maxwidth), rand(-1 * $maxheight, $maxheight));
            
$pts = count($rectanglearray) / 2;
            
imagefilledpolygon($image, $rectanglearray, $pts, $color);
            }
        else
            {
            
$centerx = rand (($maxwidth/2) - 1, $picwidth - 1 - ($maxwidth/2));
            
$centery = rand (($maxheight/2) - 1, $picheight - 1 - ($maxheight/2));
            
imagefilledellipse($image, $centerx, $centery, rand(1, $maxwidth), rand(1, $maxheight), $color);
            }

        }


    
// populate the polygon (a frame)    with paired x-y coordinates
    
$frame = array(0,0 , $picwidth - 1,0 , $picwidth - 1,$picheight - 1 , 0,$picheight - 1, 0,0);

    
// number of coordinates (pairs) in the frame
    
$pts = count($frame)/2;

    
// set the current foreground color to RGB (255, 0, 0) which is RED
    
$color = imagecolorallocate ($image, 255, 0, 0);

    
// draw the frame on the image using current color
    
imagepolygon($image, $frame, $pts, $color);

    
//imagettftext($image, 20, 45, 15, 150, $color, $font, $str);
    
$color = imagecolorallocate ($image, 0, 0, 0);
    
$sizemod = $picwidth / 1000;
    
imagettftext($image,50 * $sizemod,45,$picwidth * .65,$picheight,$color,'../fonts/arial.ttf',"Peter Mackenzie");


    
// tell the server that a PNG image  follows
    
header("Content-type: image/png");

    
// display the image
    
imagepng($image);

    
// destroy the image (i.e., release memory)
    
imagedestroy ($image);


?>
 

CODE FOLLOWS

<?php
    
include('includes/header.php');

    
$step = isset($_POST['step']) ? $_POST['step'] : 0;

    
$size = isset($_POST['size']) ? $_POST['size'] : ".5";
    
$objectsize = isset($_POST['objectsize']) ? $_POST['objectsize'] : ".25";
    
$circleprob = isset($_POST['circleprob']) ? $_POST['circleprob'] : "50";
    
$size = floatval($size);
    
$objectsize = floatval($objectsize);
    
$circleprob = intval($circleprob);


    
$self = basename($_SERVER['SCRIPT_NAME']);

?>
    <h1>Peter Mackenzie's Random Parallelogram And Circle Picture Generator</h1>

<form action="<?php echo($self); ?>" method="POST">
    <table style="width:50%">
        <tr>
            <td class="right">
                Picture Size:
            </td>
            <td class="left">
                <select name="size">
                    <option value = ".25"<?php if ($size == .25) echo('selected="selected"'); ?>>Small</option>
                    <option value = ".5"<?php if ($size == .5) echo('selected="selected"'); ?>>Medium</option>
                    <option value = "1"<?php if ($size == 1.0) echo('selected="selected"'); ?>>Large</option>
                </select>
            </td>
        </tr>
        <tr>
            <td class="right">
                Object Size:
            </td>
            <td class="left">
                <select name="objectsize">
                    <option value = ".10"<?php if ($objectsize == .1) echo('selected="selected"'); ?>>Small</option>
                    <option value = ".25"<?php if ($objectsize == .25) echo('selected="selected"'); ?>>Medium</option>
                    <option value = ".5"<?php if ($objectsize == .5) echo('selected="selected"'); ?>>Large</option>
                </select>
            </td>
        </tr>
        <tr>
            <td class="right">
                Circle Probability:
            </td>
            <td class="left">
                <select name="circleprob">
                    <?php
                        
For ($x=0;$x<101;$x++)
                            {
                            
$selected = '';
                            if (
$circleprob == $x)
                                {
                                
$selected = "selected=\"selected\"";
                                }

                            echo (
"<option value = \"$x\" $selected>$x%</option>");
                            }

                    
?>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="submit" value="Submit">
            </td>
        </tr>
    </table>
<?php
    $gettext 
= "graphics.php?picwidth=" . 800 * $size . "&picheight=" . 800 * $size . "&circleprob=$circleprob&objectsize=$objectsize";
    echo(
"<img src=\"$gettext\"<br>");

?>


<?php
    
echo("<hr>");
    
highlight_file("graphics.php");
    include(
'includes/footer.php');
?>