How to add background color with my this code in php image create from text to image?

354 Views Asked by At
<?php


    function create_image($user)
    {

        global $img_source; global $render_folder;
        global $id; global $folder;
        $quality = 9;
        $im = imagecreatefrompng($img_source);
        $clr=""; 
        foreach ($user as $value)
        {

            if(!empty($_SESSION['img_name']))
            {
                $im = imagecreatefrompng($_SESSION['img_name']);
            }
            $img_width=imagesx($im);    
            list($r, $g, $b) = sscanf($value['color'], "#%02x%02x%02x");
            $clr=imagecolorallocate($im, $r, $g, $b);

            $text_a = explode(' ', $value['name']);
            $text_new = ''; $width = $value['box-width']; $linex=1;
            foreach($text_a as $word)
            {
                //Create a new text, add the word, and calculate the parameters of the text
                $box = imagettfbbox($value['font-size'], 0, $folder.$value['font'], $text_new.' '.$word);
                //if the line fits to the specified width, then add the word with a space, if not then add word with new line
                //echo $box[2]." ";
                if($box[2] > $width){
                    $text_new .= "\n".$word; $linex++;  //echo "<br>";
                } else { 
                    $text_new .= " ".$word;
                }
            }
            $text_new = trim($text_new);





                $linslikh=explode("\n",$text_new); $ff=1; 
                foreach($linslikh as $linslikh1)
                {


                    if($value['center']=="Y")
                    {
                        //die();
                $xpos = center_text($linslikh1, $folder.$value['font'], $value['font-size'],$width);
                $xpos+=$value['x'];

                    }
                    else
                    {
                        $xpos = $value['x'];
                    }

                    $acfont = $value['font-size']*$ff ; $ypos = $acfont + $value['y'];

                    $ff=$ff+2;


                    imagettftext($im, $value['font-size'], 0, $xpos, $ypos,$clr, $folder.$value['font'],$linslikh1);


                // create the image


                }

                $output_filename = $id.'_render.png';
                imagepng($im, $render_folder.$output_filename, $quality);
                imagedestroy($im);
                $_SESSION['img_name']=$render_folder.$output_filename;
                $_SESSION['render_name']= $output_filename;




        }

    //die();
    }

    function center_text($string, $font, $font_size, $image_width){
        $dimensions = imagettfbbox($font_size, 0, $font, $string);
        return ceil(($image_width - $dimensions[4]) / 2);
    }

     ?>

Data Input File Code ( From Where Data Input)

 <div class="col-sm-1">

            <input required type="text" value="FF8C66"  class="jscolor form-control input-lg" name="clr[]" onChange="setColor(this.value,<?php echo $row; ?>);" >
       </div>

       <div class="col-sm-1">

            <input required type="text" value="FF8C66"  class="jscolor form-control input-lg" name="Bclr[]" onChange="setBColor(this.value,<?php echo $row; ?>);" >
       </div>

(Data stored from this file ) Other File Code

$new_array[]=array("name"=>$_POST['text'][$i],
                                "font-size"=>$_POST['font-size'][$i],
                                "color"=>$_POST['clr'][$i],
                                "background-color"=>"#".$_POST['Bclr'][$i],

Above is part of my code from some files . I just want to add background color option in my image. im able to add option and get data. but unable to add output in my php file which make image from input. Hope you understand my point . please help my how i can add background color from input value to in my php file . Sorry for bad English.

Update : i tried this with this way but no luck

 //
//    other code part
// 

    $Bclr="";
        $clr=""; 
                    foreach ($user as $value)
//
//    other code part
// 
    list($r, $g, $b) = sscanf($value['Bcolor'], "#%02x%02x%02x");
                    $Bclr=imagecolorallocate($im, $r, $g, $b);
    //
   // Other Code Part 
   // 
    imagettftext($im, $value['font-size'], 0, $xpos, $ypos,$clr, $Bclr,$folder.$value['font'],$linslikh1);
2

There are 2 best solutions below

1
Szue On

Create another image with imagecreatetruecolor the same size as your image, allocate backgroung color(rgb), use imagecopyresampled to merge the two images

0
Szue On

Actually, an easier method is explained in the manual :- imagecreatetruecolor imagecolorallocate then create imagefilledrectangle using the colors from imagecolorallocate, then write your text to the image. See example from Php.net below http://php.net/manual/en/function.imagettfbbox.php