Imagick SVG无法加载图像 [英] Imagick SVG cannot load the image

查看:37
本文介绍了Imagick SVG无法加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了由PHP Imagick库将SVG转换为图像的问题。 以下是我的代码:

$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg style="overflow: hidden; position: relative;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="754" version="1.1" height="565">
    <defs></defs>
    <image transform="matrix(1,0,0,1,0,0)" preserveAspectRatio="none" x="0" y="0" width="754" height="565" xlink:href="http://1439.demo.tekk3.com/wp-content/uploads/2012/10/capapix_Harley_Davidson_FLSTCI_-_Heritage_Classic.jpg"></image>
</svg>';

$im = new Imagick();
$im->readImageBlob($svg);
$im->setImageFormat("jpeg");
$im->writeimage($attached_file);
$im->clear();
$im->destroy();

,结果是一个只有白色背景的图像。没有SVG显示的任何其他图像。

如果我将Text标记放入SVG字符串中,则只有文本以白色背景呈现。图像仍然缺失。

我已经安装了php5-Imagick、libxml2、Librsvg2-bin

是否需要安装其他扩展才能获得正确的结果? 或者我的代码中有什么问题?

推荐答案

我找到了解决方法。我使用Base64镜像内容安装镜像路径。

$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
        <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="754" version="1.1" height="565">
        <image x="0" y="0" width="754" height="565" xlink:href="'.imageToBase64('icons-weather_01.png').'"></image>
    </svg>';

function imageToBase64($path) {
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    return 'data:image/' . $type . ';base64,' . base64_encode($data);
}

如果可能的话,可以使用本地图像或Web URL。

这篇关于Imagick SVG无法加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆