不可思议的SVG到PNG给出了空白图片PHP

问题描述

SVG内容来自服务的json数据。这是我的svg文件

$data = <div style='width: 100%;'>
<svg xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:rdf='http://www.w3.org/1999/02/22-rdf-Syntax-ns#' xmlns:svg='http://www.w3.org/2000/svg' id='svg278' version='1.1' width='420' height='420' viewBox='0 0 420 420' xmlns='http://www.w3.org/2000/svg' style='width: 100%;'>
  <image href='BASE54IMAGE' width='420' height='420'>
  </image>
  <g transform='translate(124.6875,70)'>
  <g>
     <rect rx='3' width='161' height='280' class='no-fill' fill='transparent' stroke='#000000' stroke-width='1.25'></rect>
  </g>
  <g transform='translate(16.1,28) rotate(0,64.4,64.4)' class='cursor-pointer'>
     <g data-purpose='color-fill'>
        <mask id='0-0'>
           <image href='BASE64IMAGE' width='128.8' height='128.8'></image>
        </mask>
        <mask id='0-1'>
           <image href='BASE64IMG' width='128.8' height='128.8'></image>
        </mask>
        <rect rx='3' mask='url(#0-0)' fill='#FFFFFF' width='128.8' height='128.8'></rect>
        <rect rx='3' mask='url(#0-1)' fill='#abe0f5' width='128.8' height='128.8'></rect>
     </g>
     <g data-purpose='configurations'>
        <g transform='translate(-25,-25)'>
           <rect class='no-fill cursor-pointer' fill='transparent' stroke='#000000' stroke-width='1.25' width='25' height='25'></rect>
        </g>
        <g transform='translate(128.8,-25)'>
           <rect class='no-fill cursor-pointer' fill='transparent' stroke='#000000' stroke-width='1.25' width='25' height='25'>r</rect>
        </g>
        <g transform='translate(128.8,128.8)'>
           <rect class='no-fill cursor-pointer' fill='transparent' stroke='#000000' stroke-width='1.25' width='25' height='25'></rect>
        </g>
        <g transform='translate(-25,128.8)'>
           <rect class='no-fill cursor-pointer' fill='transparent' stroke='#000000' stroke-width='1.25' width='25' height='25'></rect>
        </g>
     </g>
     <g data-purpose='cover-color-fill'>
        <rect rx='3' class='no-fill cursor-pointer' fill='transparent' stroke='#000000' stroke- 
 width='1.25' width='128.8' height='128.8'></rect>
     </g>
  </g>
  </g>
   </svg>
  </div>

在这里解码内容

$data = json_encode($json,true);
$htmlData = htmlentities($data);
$htmlContent = htmlspecialchars_decode($htmlData);

另存为svg:

file_put_contents('assets/pdfs/new.svg',$htmlContent);   

保存效果很好,我可以打开svg,没有任何错误

尝试在此处将我保存的svg转换为png,

        $imPng = new \Imagick();
        $png = file_get_contents('assets/pdfs/new.svg');
        
        $png = '<?xml version="1.0"  encoding="UTF-8" standalone="no"?>' .$png;
        $imPng->readImageBlob($png);
        $imPng->setimageFormat("png");
        $imPng->writeImage('new.png');
        file_put_contents ('assets/pdfs/new.png'); 

但是转换后的PNG为空白。我该怎么办 ?该代码有什么问题?

注意:在svg内容中,所有图像都以base64格式出现

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)