如何解决不可思议的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 举报,一经查实,本站将立刻删除。