Parse variable value to imagefill color in php

59 Views Asked by At

im creating a image generator with php and i have a problem.

I have a form that asks for color that's already defined

    <select name="background color">
                        <option value=" 255, 0, 0">Red</option>
                        <option value="0,128,0">Green</option>
                        </select> 

Once i select it and do my thing it gets stored in a variable with the POST method

?php

$height=$_POST['height'];
$width=$_POST['width'];
$cbackground=$_POST['backgroundcolor'];
$ctext=$_POST['text color'];
$text=$_POST['text'];
$arr = get_defined_vars();

header('Content-Type:image/jpeg');
$img = imagecreatetruecolor($width, $height);
imagefill($img, 0, 0, 255, 545, 543); 

as you can see in the last line i hard coded a color, but what i want to acomplish is to call the value from the cbackground variable like this or similar.

imagefill($img, 0, 0, $cbackground);

when i do this i only get a little grey square, so it doesnt work as intended.

There is any way to make it work? properly?

1

There are 1 best solutions below

0
On

So what you can use is use the value part of :

   <select name="backgroundcolor">
      <option value=" 255, 0, 0">Red</option>
      <option value="0,128,0">Green</option>
   </select> 

And explode the string to get an array with the 3 numbers :

$valueArray = explode(",",str_replace(" ","",$_POST['backgroundcolor']));

Now you have an array made of :

  [0] => 255, 
  [1] => 0, 
  [2] => 0

So you just have to use it like so :

imagefill($img, 0, 0, $valueArray[0], $valueArray[1], $valueArray[2]);