1
<?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);
    }

     ?>

数据输入文件代码(从哪里输入数据)

 <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>

(从这个文件存储的数据) 其他文件代码

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

以上是我的一些文件中的代码的一部分。我只想在我的图像中添加背景颜色选项。我能够添加选项并获取数据。但无法在我的 php 文件中添加从输入生成图像的输出。希望你明白我的意思。请帮助我如何将背景颜色从输入值添加到我的 php 文件中。抱歉英语不好。

更新:我用这种方式尝试过,但没有运气

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

2 回答 2

0

实际上,手册中解释了一种更简单的方法:- imagecreatetruecolor imagecolorallocate 然后使用 imagecolorallocate 中的颜色创建 imagefilledrectangle,然后将文本写入图像。请参阅下面的 Php.net 示例 http://php.net/manual/en/function.imagettfbbox.php

于 2019-01-12T08:19:05.810 回答
0

用imagecreatetruecolor创建另一个与你的图像大小相同的图像,分配背景颜色(rgb),使用imagecopyresampled合并两个图像

于 2019-01-12T06:43:00.350 回答