如何在文件夹中生成svg并使用php强制下载为svg文件?
我已经使用php在html中生成svg输出,当我单击download as svg时,必须强制将其作为svg图像下载。如何在单击下载时将其生成为svg图像如何在文件夹中生成svg并使用php强制下载为svg文件?,php,svg,Php,Svg,我已经使用php在html中生成svg输出,当我单击download as svg时,必须强制将其作为svg图像下载。如何在单击下载时将其生成为svg图像 foreach($items as $item){ // echo $item->top; $top = str_replace('px', '', $item['top']); $left = str_replace('px', '', $item['left']); echo '<div
foreach($items as $item){
// echo $item->top;
$top = str_replace('px', '', $item['top']);
$left = str_replace('px', '', $item['left']);
echo '<div style="background-color:#fff;width:333.333px;height:500px;">';
echo '<div style="position:absolute;top:'.$top.';left:'.$left.'">';
$svg_element = $item['svg'];
echo $svg_element;
echo '</div>';
echo '</div>';
}
// output
<div style="background-color:#fff;width:333.333px;height:500px;"><div style="position:absolute;top:235;left:64.0667">
<svg width="198" height="132" viewBox="0 0 99 52" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="none">
<defs><style>@font-face{font-family: 'Twine';src: url("data:application/font-ttf;charset=utf-8;base64,")
<g id="0.38592716854139286"><text fill="#000000" stroke="none" stroke- width="0" stroke-linecap="round" stroke-linejoin="round" x="32" y="39" text-anchor="middle" font-size="40.30555555555556" font-family="Twine" data-textcurve="0" data-itemzoom="1 1" itemzoom="0.5 0.3939393939393939"> <tspan dy="0" x="50%">Hello</tspan></text></g>
foreach($items作为$item){
//echo$item->top;
$top=str_替换('px','',$item['top']);
$left=str_替换('px','',$item['left']);
回声';
回声';
$svg_element=$item['svg'];
echo$svg_元素;
回声';
回声';
}
//输出
@字体面{font-family:'Twine';src:url(“数据:应用程序/font-ttf;字符集=utf-8;base64,”)
你好
对于SVG,不应该有任何div。因此,我删除了div,创建了具有宽度和高度、背景颜色的SVG,并将内容放在文件中,另存为.SVG扩展名
$op = '<svg width="'.$template_width.'" height="'.$template_height.'">';
foreach($items as $item){
// echo $item->top;
$top = str_replace('px', '', $item['top']);
$left = str_replace('px', '', $item['left']);
$op .= str_replace('<svg ', '<svg y="'.$top.'" x="'.$left.'" ', $item['svg']);
}
$op .= '</svg>';
$filename = '/front_'.time().'.svg';
file_put_contents(realpath(dirname(__FILE__)).$filename, $op);
$op='';
foreach($items作为$item){
//echo$item->top;
$top=str_替换('px','',$item['top']);
$left=str_替换('px','',$item['left']);
$op.=str_replace('您找到解决方案了吗?