Alter the Width of a Picture


Current Image (courtesy of Veronica Hutchins)

ocean image

Select Width Change (-5 = Wider, 0 = No Change, 5 = Narrower)

Width Determination:


-5   -4   -3   -2   -1   0   1   2   3   4   5  





SLAVE CODE FOLLOWS

<?php // narrow script

$narrow $_GET['sk'];

// get the images from files
$image imagecreatefromjpeg("../images/jason-leung-Xaanw0s0pMk-unsplash_sm.jpg");

// get sizes of original
$width_orig imagesx($image);
$height imagesy($image);

// adjust the width for the new image
$width $width_orig - ($width_orig $narrow .05);

// create a new image
$new_image imagecreatetruecolor($width$height);

// resample old to new image
imagecopyresampled($new_image$image0000$width$height$width_orig$height);

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

?>
 

CODE FOLLOWS

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

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

$s 'CHECKED';

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

?>

    <h2>
        Alter the Width of a Picture
    </h2>

<?php

switch ($step)
{

    
// STEP 1

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

        <hr>
        <p>
            Current Image (courtesy of Veronica Hutchins)
        </p>

        <img src="../images/ocean.jpg" alt="ocean image">

        <p>
            Select Width Change (-5 = Wider, 0 = No Change, 5 = Narrower)
        </p>

        <form action="<?php echo($self); ?>" method="post">
            <fieldset>
                <legend>Width Determination:</legend>
                <br>
                <label for="choice">Wider to Narrower</label>
                <br><br>
                <?php
                
for ($i = -5$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>


        <?php
        
break;

    
// STEP 2

    
case 2:    // show the new width

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

        <hr>

        <h3> Narrowness: <?php echo($narrow);?></h3>

        <img src="change_image_width.php?sk=<?php echo($narrow);?>" alt="My Narrow Image">

        <br><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
// code for slave routine
echo('<hr><br>SLAVE CODE FOLLOWS<br><br>');
highlight_file ('narrow.php');

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