Change the Brightness of a Picture


Current image taken from images file folder:

my Dog

Select Brightness (0 = no change, 10 = brightest)

Brightness Determination:

0   1   2   3   4   5  




NOTE: The instructions below are NOT part of your assignment. As such, they do not need to be included in your final page.

Your assignment:

Create a form that works as this one does.

Note this is a TWO STEP Form operation:

Form Step 1

Show the image (taken directly from the filesystem with sticky radio buttons ranging from 0 to 10. These radio buttons will allow the user to select the degree of brightness they want to apply to the image and then click Submit.

Form Step 2

Send the script Brightness values via a GET to the image slave brightness script (see below). This script ("bright.php"): 1) gets the image from the file system; 2) adjust the brightness of the image; 3) and then displays the image in part two of the form.

Also, provide the user with the option to do it again.



SLAVE CODE FOLLOWS

<?php // brightness script

    // this takes an image from a database and changes it's bightness

    
$brightness $_GET['br'];

    
// get the images from files
    
$image imagecreatefromjpeg("images/baby.jpg");

    
// get the palette from the image and create a 256 color palette
    
imagetruecolortopalette($imagetrue256);

    
// find the total number of colors in the palette
    
$totalColors imagecolorstotal($image);

    
//    now cycle through all the colors and change their brightness (see function)
    
for ($index 0$index $totalColors$index++)
        {
        
// $RGB is an array
        
$RGB imagecolorsforindex($image$index);
        
$red adjustBightness($RGB['red'], $brightness);
        
$green adjustBightness($RGB['green'], $brightness);
        
$blue adjustBightness($RGB['blue'], $brightness);
        
imagecolorset($image$index$red$green$blue);
        }

    
// set the header for the image
    
header("Content-type: image/jpeg");
    
imagejpeg($image);
    
imagedestroy($image);

    
// ==== functions =======

    
function adjustBightness($color$brightness)
        {
        
// determine the color brightness
        
$color += (($brightness 10) * 255);

        
// make sure the color value does not exceed 255
        
$color = ($color 255) ? 255 $color;
        return 
$color;
        }

?>

 

CODE FOLLOWS

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

    
$self basename($_SERVER['SCRIPT_NAME']);
    
$step = isset($_POST['step']) ? $_POST['step'] : 1;
    
$brightness = isset($_POST['choice']) ? $_POST['choice'] : null;

    
$s 'CHECKED';

    if (isset(
$_POST['choice']))
        {
        
$choice $_POST['choice'];
        }
    else
        {
        
$choice 0;    // set default here
        
}
?>

    <h1>
        Change the Brightness of a Picture
    </h1>

<?php

    
switch ($step)
        {

        
// STEP 1 ----------------------

        
case 1:    //--- get the change of brightness the user wants
            
?>

                <hr>
                <p>
                    Current image taken from images file folder:
                </p>

                <img src="images/baby.jpg" alt="my Dog">

                <p>
                    Select Brightness (0 = no change, 10 = brightest)
                </p>

                <form action="<?php echo($self); ?>" method="post">
                    <fieldset>
                        <legend>Brightness Determination:</legend>
                        <label for="choice">Bright</label>
                        <br><br>
                        <?php
                            
for ($i 0$i <= 5$i++)
                                {
                                
?>

                                    <input type="radio" id="choice" name="choice"
                                           value="<?php echo($i); ?>"<?php if ($choice == $i) {
                                    echo(
$s);
                                    } 
?> > <?php echo($i); ?> &nbsp;

                                    <?php
                                
}
                        
?>
                    </fieldset>
                    <br>
                    <br>
                    <input type="submit" name="submit" value="Submit">
                    <input type="hidden" name="step" value="2">
                </form>
                <br>

                <hr>

                <p class="red">
                    NOTE: The instructions below are NOT part of your assignment. As such, they do not
                    need to be included in your final page.
                </p>

                <h2>
                    Your assignment:
                </h2>

                <p>
                    Create a form that works as this one does.
                </p>

                <ul>
                    <li>The Sticky Radio Example;</li>
                    <li>AND the Two Step Form from that Example;</li>
                    <li>The slave script is listed below. You don't need to alter the code,
                        just use it correctly "as-is".
                    </li>
                </ul>

                <h2>
                    Note this is a TWO STEP Form operation:
                </h2>
                <p>
                    Form Step 1
                </p>
                <p>
                    Show the image (taken directly from the filesystem with sticky radio buttons ranging from 0 to 10.
                    These radio buttons will allow the user to select the degree of brightness they want to apply to
                    the image and then click Submit.
                </p>
                <p>
                    Form Step 2
                </p>
                <p>
                    Send the script Brightness values via a GET to the image slave brightness script (see below).
                    This script ("bright.php"): 1) gets the image from the file system; 2) adjust the brightness
                    of the image; 3) and then displays the image in part two of the form.
                </p>
                <p>
                    Also, provide the user with the option to do it again.
                </p>

                <?php
                
break;

        
// STEP 2 ----------------------

        
case 2:    //--- show the brightness

            // provide the user the opportunity to try again
            
?>

                <hr>

                <h3> Brightness: <?php echo($brightness); ?></h3>

                <img src="bright.php?br=<?php echo($brightness); ?> alt="My Bright Dog">

        <br>                     

        <form action="<?php echo($self); ?>" method="post">
            <input type="hidden" name="step" value="1">
            <input type="submit" value="Try Again">
        </form>

        <br> 
            
        <?php
                
break;
        }

    
// end of program
    // list code for slave routine
    
echo('<hr><br>SLAVE CODE FOLLOWS<br><br>');
    
highlight_file('bright.php');
    echo(
'<hr>');

    include(
'includes/footer.php');
?>