<?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);
答案 0 :(得分:0)
使用imagecreatetruecolor创建与图像大小相同的另一幅图像,分配背景色(rgb),使用imagecopyresampled合并两个图像
答案 1 :(得分:0)
实际上,手册中介绍了一种更简单的方法: imagecreatetruecolor imagecolorallocate 然后使用imagecolorallocate中的颜色创建imagefilledrectangle, 然后将您的文字写到图像上。 请参阅下面来自Php.net的示例 http://php.net/manual/en/function.imagettfbbox.php