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   6   7   8   9   10  






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/ollie.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/ollie.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 <= 10$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>

     <?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');
?>