changing color options in php.

This is a discussion on "changing color options in php." within the PHP Forum section. This forum, and the thread "changing color options in php. are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 18th, 2007, 18:18
SuperMember

SuperMember
Join Date: Sep 2006
Location: Pink House
Posts: 3,946
Thanks: 0
Thanked 0 Times in 0 Posts
changing color options in php.

I think this will be the last question for this site. Although I wouldn't make a bet on this.

The problem lies on this page. At the bottom there is an option to "select your color". Since this was done, the client has asked to change some of the colors and add some new ones.

I'm at a loss for how to do this since each time I've messed with it, I've had nothing on the page left except for graphics and css styling.

Not sure what files you will need to see to get this to work. I have a page called test2.php that has all the colors and their rgb numbers. Each time I change the colors in the test2.php things do not end up well.

I think I'll give you the code for the test2.php:
Code: Select all
<?php


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

// Set the content-type
header("Content-type: image/png");
// Create the image
$im = imagecreatetruecolor(750, 150);



//create canvas colour
$canvas = imagecolorallocate($im, 255, 223, 251);

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

//get the phrase and calculate its width for centering
$phrase = stripslashes($_GET['phrase']);

//calculate the x and y co-ordinates and set the default font size = 24
$canvas_wid = 375;
$canvas_hgt = 90;

$bbox = imagettfbbox(24, 0, $font, $phrase);

$width = abs($bbox[2] - $bbox[0])/2;
$height = abs($bbox[7]) - abs($bbox[1]);

$x1 = $canvas_wid - $width;
$y1 = $canvas_hgt - $height;

// Initialise the colors and select the correct one
$colour = $_GET['colour'];

if ($colour == 'hotpink') {
     $col = imagecolorallocate($im, 255, 105, 180);
}
elseif ($colour == 'lightpink') {
         $col = imagecolorallocate($im, 255, 182, 193);
}
elseif ($colour == 'deeppink') {
         $col = imagecolorallocate($im, 255, 020, 147);
}
elseif ($colour == 'snow1') {
         $col = imagecolorallocate($im, 255, 250, 250);
}
elseif ($colour == 'royalblue4') {
         $col = imagecolorallocate($im, 039, 064, 139);
}
elseif ($colour == 'blue4') {
         $col = imagecolorallocate($im, 000, 000, 139);
}
elseif ($colour == 'red3') {
         $col = imagecolorallocate($im, 205, 000, 000);
}
elseif ($colour == 'red4') {
         $col = imagecolorallocate($im, 139, 000, 000);
}
elseif ($colour == 'grey36') {
         $col = imagecolorallocate($im, 092, 092, 092);
}
elseif ($colour == 'grey61') {
         $col = imagecolorallocate($im, 156, 156, 156);
}
elseif ($colour == 'orangered1') {
         $col = imagecolorallocate($im, 255, 069, 000);
}
elseif ($colour == 'goldenrod1') {
         $col = imagecolorallocate($im, 255, 193, 137);
}
elseif ($colour == 'honeydew1') {
         $col = imagecolorallocate($im, 240, 255, 240);
}
elseif ($colour == 'ghostwhitematt') {
         $col = imagecolorallocate($im, 248, 248, 255);
}
elseif ($colour == 'ghostwhiteshiny') {
         $col = imagecolorallocate($im, 248, 248, 255);
}
elseif ($colour == 'blackmatt') {
         $col = imagecolorallocate($im, 000, 000, 000);
}
elseif ($colour == 'blackshiny') {
         $col = imagecolorallocate($im, 000, 000, 000);
}
elseif ($colour == 'darkturquoise') {
         $col = imagecolorallocate($im, 000, 206, 209);
}
elseif ($colour == 'limegreen') {
         $col = imagecolorallocate($im, 050, 205, 050);
}
elseif ($colour == 'saddlebrown') {
         $col = imagecolorallocate($im, 139, 069, 019);
}
elseif ($colour == 'tan') {
         $col = imagecolorallocate($im, 210, 180, 140);
}
elseif ($colour == 'beige') {
         $col = imagecolorallocate($im, 245, 245, 220);
}
else {
         $col = imagecolorallocate($im, 0, 0, 0);
}

//select a suitible font size and center it in the canvas -- This is guesswork as all phrases are different sizes
if(($width*2) > 760) {
$size = 15;
$bbox = imagettfbbox($size, 0, $font, $phrase);

$width = abs($bbox[2] - $bbox[0])/2;
$height = abs($bbox[7]) - abs($bbox[1]);

$x1 = $canvas_wid - $width;
$y1 = $canvas_hgt - $height;
}
else {
$size = 24;
}

//create a border
imagefilledrectangle($im, 1, 1, 748, 148, $canvas);

// Add the text
imagettftext($im, $size, 0, $x1, $y1, $col, $font, $phrase);

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

}
?>
Honestly I'm not even sure that's the file you'll need to direct me.

If not, I have another php that I can post the code for.

In case your view source is disabled, here is the code for the preview.php page.
Code: Select all
<?php
         if(isset($_GET["font"])) {
         $val = stripslashes($_GET['font']);
         echo "<option value=\"$val\" selected=\"selected\">$val</option>";     
         }
        ?>
         <option value="fonts/Adorable.ttf">Adorable</option>
         <option value="fonts/AMAZR___.TTF">AMAZR</option>
         <option value="fonts/angelina.TTF">angelina</option>     
         <option value="fonts/Antique Type.ttf">Antique Type</option>
         <option value="fonts/Ashley.ttf">Ashley</option>
         <option value="fonts/BLACKJAR.TTF">BLACKJAR</option>
         <option value="fonts/BOYZRGRO.TTF">BOYZRGRO</option>
         <option value="fonts/BradhITC.TTF">BradhITC</option>
         <option value="fonts/CHAMPGNE.TTF">CHAMPAGNE</option>
         <option value="fonts/Charlesworth Bold.ttf">Charlesworth Bold</option>
         <option value="fonts/ChocolateBox.ttf">Chocolate Box</option>
         <option value="fonts/CHOPS___.TTF">CHOPS</option>
         <option value="fonts/circus.ttf">Circus</option>
         <option value="fonts/comic.ttf">Comic</option>
         <option value="fonts/Curlz___.TTF">Curlz</option>
         <option value="fonts/ENGAN___.TTF">ENGAN</option>
         <option value="fonts/fontdine.ttf">Fontdine</option>
         <option value="fonts/FREEBSC_.ttf">FREEBSC</option>
         <option value="fonts/Frscript.TTF">Frascript</option>
         <option value="fonts/georgia.ttf">Georgia</option>
         <option value="fonts/GILLIGAN.TTF">GILLIGAN</option>
         <option value="fonts/GIRLW___.TTF">GIRLW</option>
         <option value="fonts/ITCEdscr.TTF">ITCEdscr</option>
         <option value="fonts/ITCKrist.TTF">ITCKrist</option>
         <option value="fonts/Kayleigh.TTF">Kayleigh</option>
         <option value="fonts/Laine.TTF">Laine</option>
         <option value="fonts/LARIAT.TTF">LARIAT</option>
         <option value="fonts/LaurenScript.ttf">LaurenScript</option>
         <option value="fonts/LemonChicken.ttf">Lemon Chicken</option>
         <option value="fonts/MA Sexy.ttf">MA Sexy</option>
         <option value="fonts/Majestic.ttf">Majestic</option>
         <option value="fonts/MTCORSVA.TTF">MTCORSVA</option>     
         <option value="fonts/MURIEL.TTF">MURIEL</option>
         <option value="fonts/NASHVILL.TTF">NASHVILL</option>
         <option value="fonts/Papyrus.TTF">Papyrus</option>
         <option value="fonts/PegsannaHMK.ttf">PegsannaHMK</option>
         <option value="fonts/RINGM___.TTF">RINGM</option>
         <option value="fonts/Santa's Sleigh.ttf">Santas Sleigh</option>
         <option value="fonts/Scriptina.ttf">Scriptina</option>
         <option value="fonts/Splendid ES.ttf">Splendid ES</option>
         <option value="fonts/SuzanneQuillSH.ttf">SuzanneQuillSH</option>
         <option value="fonts/TempsITC.TTF">TempsITC</option>
         <option value="fonts/TINKT___.TTF">TINKT</option>         
         <option value="fonts/UPCJL.TTF">UPCJL</option>
         <option value="fonts/Vivaldii.TTF">Vivaldii</option>
         <option value="fonts/wds052801.ttf">wds052801</option>
         <option value="fonts/Whackadoo.ttf">Whackadoo</option>     
         <option value="fonts/Witched.TTF">Witched</option>          
        </select>
<a href=colors_fonts.php>Click here to view our Fonts and Colors page</a>    <br />
<br /><br />

        
<h2>Select Your Color</h2><select name="colour">
        <?php
         if(isset($_GET["colour"])) {
         $val = $_GET['colour'];
         echo "<option value=\"$val\" selected=\"selected\">$val</option>";     
         }
        ?>     
          <option value="purple" class="purple">Purple</option>
         <option value="hotpink" class="hotpink">Hot Pink</option>
         <option value="lightpink" class="lightpink">Light Pink</option>
         <option value="red" class="red">Red</option>
         <option value="dkred" class="dkred">Burgundy</option>
         <option value="orangered1" class="orangered1">Orange</option>
          <option value="orangered1" class="orangered1">Orange Red 1</option>
         <option value="royalblue4" class="royalblue4">Royal Blue 4</option>
         <option value="blue4" class="blue4">Blue 4</option>
        
         
         <option value="grey36" class="grey36">Grey 36</option>
         <option value="grey 61" class="grey 61">Grey 61</option>
        
         <option value="goldenrod1" class="goldenrod1">Golden Rod 1</option>
         <option value="honeydew1" class="honeydew1">Honey Dew 1</option>
         <option value="ghostwhitematt" class="gostwhitematt">Ghost White Matt</option>
         <option value="ghostwhiteshiny" class="ghostwhiteshiny">Ghost White Shiny</option>
         <option value="blackmatt" class="blackmatt">Black Matt</option>
         <option value="blackshiny" class="blackshiny">Black Shiny</option>
         <option value="darkturquoise" class="darkturquoise">Dark Turquoise</option>
         <option value="limegreen" class="limegreen">Lime Green</option>
         <option value="saddlebrown" class="saddlebrown">Saddle Brown</option>
         <option value="tan" class="tan">Tan</option>
         <option value="beige" class="biege">Biege</option>         
        </select>
<br />
         
         
         <input name="submit" type="submit" value="Update Preview" /><br />
      </form>    <br />
<br /></div>
  
<?php
$default = "Your text will appear here";
if(isset($_GET["phrase"])) {
 $p = stripslashes($_GET["phrase"]);
 $f = stripslashes($_GET["font"]);
 $c = $_GET['colour'];

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

 echo "<img src=\"test2.php?phrase=$default&font=fonts/Charlesworth%20Bold.ttf&colour=black\" alt=\"\" />";
}
?>
The code in red is the part that I need to update at this time.
Thanks for your help!!

Linda
Reply With Quote

  #2 (permalink)  
Old Sep 18th, 2007, 19:39
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: changing color options in php.

Quote:
Originally Posted by Lchad
In case your view source is disabled, here is the code for the preview.php page.
LOL

Okay so all the if's/elses are creating a colour for use in the PHP GD module.

The numbers that you see are RGB values

$col = imagecollorallocate($im, red, green, blue);

So....

PHP: Select all

if ($colour == 'hotpink') {

     
$col imagecolorallocate($im255105180);
}
elseif (
$colour == 'lightpink') {
         
$col imagecolorallocate($im255182193);
}
elseif (
$colour == 'deeppink') {
         
$col imagecolorallocate($im255020147);

// to add another colour, you can squeeze it in here
elseif ($colour == 'yuckybrown') {
        
$col imagecolorallocate($im724325);
}
// the rest of the code continues
elseif ($colour == 'tan') {
         
$col imagecolorallocate($im210180140);
}
elseif (
$colour == 'beige') {
         
$col imagecolorallocate($im245245220);
}
else {
         
$col imagecolorallocate($im000);

The it would simply be a matter of adding an option the the select elemen with the value set as the new colour.

In the example above, that would be:

<option value="yuckybrown" class="yuckybrown">Icky Brown Colour</option>

And then creating a class in your stylesheet to show the option in the desired colour.

Hope that helps.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Sep 19th, 2007, 18:06
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: changing color options in php.

I suspect you have a style sheet definition for each colour too, judging by that "class=" ....


Really - sorry - I have to say this "If you find yourself repeating code then there MUST be an easier way". That code would be so much better with the colour defined in an include file (or even a data file of database) ...

Code: Select all
$colourtable = array(
"hotpink" => array("Hot Pink",205,105,180),
"lightpink" => array("Light Pink",255,182,193) ... 
etc
And you can then include that file wherever it's needed. Your entire elseif monstrosity is replace by

Code: Select all
$ct = $colourtable[$colour);
if (! $ct) $ct = $colourtable["black"];
$col = imagecolorallocate($im,$ct[1],$ct[2],$ct[3]);
and the entire option block by

Code: Select all
foreach (array_keys($colourtable) as $col) {
        $ct = $colourtable[$col];
        print "<option value=$col class=$col>$ct[0]</option>";
        }
I guess this might be an old application you can't clean up ???
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Blog options whym Webforumz Suggestions and Feedback 7 Sep 30th, 2007 23:07
Color Changing Logo thresherpirate Scripts and Online Services 10 Jun 23rd, 2007 08:13
Color Changing Logo thresherpirate Flash & Multimedia Forum 7 Jun 23rd, 2007 07:54
Help changing page setup options at runtime just_the_basix ASP.NET Forum 1 Jul 20th, 2004 18:45


All times are GMT. The time now is 07:30.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43