0

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

4

0 回答 0