Jump to content

Recommended Posts

I have a script that allows a user to selecta  phrase, a font, and a colour of that font and see it previewed in a window below the form.

 

I cant seemt o gett he colour selection to work at all, at the moment it just keeps showing up in red.

 

heres the script

 

First the form

      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<h2>Enter Your Phrase</h2>
  <input type="text" name="phrase" value="<?php 
	 if(isset($_POST["phrase"])) {
	 echo $_POST["phrase"];
	 }		 
	 ?>"/>


         <br />
<h2>Select Your Font</h2>
<select name="font">
	 <option value="fonts/Adorable.ttf">Adorable</option>
	 <option value="fonts/angelina.TTF">angelina</option>
	 <option value="fonts/Ashley.ttf">Ashley</option>
    </select><br />

<h2>Select Your Color</h2>
<select name="colour">
	 <option value="red" selected="selected" class="red">Red</option>
	 <option value="pink" class="pink">Pink</option>
	 <option value="blue" class="blue">Blue</option>
    </select><br />


         <input type="submit" /><br />
      </form>
  
  
<?php
if(isset($_POST["phrase"])) {
$p = $_POST["phrase"];
$f = $_POST["font"];
$c = $_POST['colour'];
echo "<img src=\"test2.php?phrase=$p&font=$f&colour=$c\" alt=\"\" />";
}
?>

 

And now the script to create the image

<?php


if(isset($_GET['phrase'])) {

// Set the content-type
header("Content-type: image/png");

$phrase = stripslashes($_GET['phrase']);

// Create the image
$im = imagecreatetruecolor(400, 100);

// Create some colors
$colour = $_GET['colour'];
if ($colour = 'red') {
	$col = imagecolorallocate($im, 255, 000, 000);
}
elseif ($colour = 'pink') {
 	$col = imagecolorallocate($im, 255, 105, 180);
}
elseif ($colour = 'blue') {
 	$col = imagecolorallocate($im, 000, 000, 255);
}
else {
 	$col = imagecolorallocate($im, 0, 0, 0);
}


$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);

//create a border
imagefilledrectangle($im, 1, 1, 398, 98, $white);

// define the path to the selected font
$font = $_GET['font'];

// Add the text
imagettftext($im, 20, 0, 50, 50, $col, $font, $phrase);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

}
?>

 

Can anyone see what im doing wrong? .... my if statements look right to me..... but i cant get the result i expect.

 

Any tips really appreciated on this.... thanks guys

Link to comment
https://forums.phpfreaks.com/topic/53729-solved-colour-selection-problem/
Share on other sites

Try changing the first piece of code posted to:

 

<?php
if(isset($_POST)) {
	$phrase = $_POST['phrase'];
	$font = $_POST['font'];
	$colour = $_POST['colour'];
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<h2>Enter Your Phrase</h2>
<input type="text" name="phrase" value="<?php if(isset($phrase)) { echo $phrase; } ?>"/>

<br />

<h2>Select Your Font</h2>
<select name="font">
<option value="fonts/Adorable.ttf">Adorable</option>
<option value="fonts/angelina.TTF">angelina</option>
<option value="fonts/Ashley.ttf">Ashley</option>
</select>

<br />

<h2>Select Your Color</h2>
<select name="colour">
<option value="red" class="red">Red</option>
<option value="pink" class="pink">Pink</option>
<option value="blue" class="blue">Blue</option>
</select>

<br />

<input type="submit"/><br />

</form>
  
  
<?php

if(isset($phrase)) {
$p = $phrase;
$f = $font;
$c = $colour;
echo "<img src=\"test2.php?phrase=$p&font=$f&colour=$c\" alt=\"\" />";
}

?>

 

Chris.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.