Alter the Width of an Image and Add Text


Current Image (courtesy of Veronica Hutchins)

beach image

Select Width Change (-5 = Widest, 0 = no change, 5 = Narrowest)

Width Determination:


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

Your Text




SLAVE CODE FOLLOWS

<?php // resize image

$user_string $_GET['sk'];

$user_array explode(","$user_string);
$narrow $user_array[0];
$word $user_array[1];

// 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);

// font
$font "fonts/arial.ttf";

// font foreground color
$color imagecolorallocate($image1702550);

// combines the image with text using the following specs
imagettftext($image241820$height 12$color$font$word);

// 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");

// display the image
imagejpeg($new_image);

// destroy the image (i.e., release memory)
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;
$word = isset($_POST['user']) ? $_POST['user'] : "Surprise";

$s 'CHECKED';

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

?>

<h2>
    Alter the Width of an Image and Add Text
</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/closeup_beach.jpg" alt="beach image">

        <p>
            Select Width Change (-5 = Widest, 0 = no change, 5 = Narrowest)
        </p>

        <form action="<?php echo($self); ?>" method="post">
            <fieldset>
                <legend>Width Determination:</legend>
                <br>
                <label for="choice">Alter Width</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>
            <fieldset>
                <legend>Your Text</legend><br>
                <label for="string">Type a word or short phrase: </label>
                <input type="text" size="36" id="user" name="user" value="">
            </fieldset>
            <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

        // offer to try again
        
?>

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

        <img src="text_resize_image.php?sk=<?php echo($narrow "," $word);?>" 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
// list code for slave routine
echo('<hr><br>SLAVE CODE FOLLOWS<br><br>');
highlight_file ('text_narrow_image.php');
//echo('<hr>');

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