PHP IMagick:无法保存图像

时间:2015-04-03 17:19:29

标签: php imagick

我想将SVG转换为JPG,根据Convert SVG image to PNG with PHP我创建的转化方法

public static function createJpgFromSvg($src, $dst, $w = 320, $h = 240) {
  $im = new \Imagick();
  $svg = file_get_contents($src);
  $im->readImageBlob($svg);  
  $im->setImageFormat("jpeg");
  $im->adaptiveResizeImage($w, $h);
  $im->writeImage($dst);
  $im->clear();
  $im->destroy();   
}

问题是我总是收到一个例外:

ImagickException #1

unable to open image `<path>': No such file or directory @ error/blob.c/OpenBlob/2675

对于行:

$im->writeImage($dst);

我已经检查了目标文件夹的写权限,我有777.你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您很可能正在尝试阅读ImageMagick认为不是有效SVG的内容。

这是一个应该有效的例子:

function svgExample() {   
    $svg = '<!--?xml version="1.0"?-->
    <svg width="120" height="120" viewport="0 0 120 120" version="1.1" xmlns="http://www.w3.org/2000/svg">

        <defs>
            <clipPath id="myClip">
                <circle cx="30" cy="30" r="20"></circle>
                <circle cx="70" cy="70" r="20"></circle>
            </clipPath>
        </defs>

        <rect x="10" y="10" width="100" height="100" clip-path="url(#myClip)"></rect>

    </svg>';

    $image = new \Imagick();

    $image->readImageBlob($svg);
    $image->setImageFormat("jpg");
    header("Content-Type: image/jpg");
    echo $image;
}

您可以通过将SVG文本与您拥有的内容进行比较来找出问题所在。如果你不能,你需要在某个地方发布你的SVG文件,以便让其他人看到它。