简体   繁体   English

PHP IMagick:无法保存图像

[英]PHP IMagick: cannot save image

I want to convert SVG to JPG, according to Convert SVG image to PNG with PHP I've created conversion method 我想将SVG图像转换为JPG,根据我用PHP创建SVG图像转换为PNG的转换方法

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();   
}

Problem is that I always receive an exception: 问题是我总是会收到一个例外:

ImagickException #1

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

For line: 对于行:

$im->writeImage($dst);

I've already checked write rights on the destination folder and I have 777. Could you help me? 我已经检查了目标文件夹上的写权限,并且我有777。您能帮帮我吗?

It's very likely that you're trying to read something that ImageMagick doesn't consider to be valid SVG. 您很有可能正在尝试阅读ImageMagick认为不是有效SVG的内容。

Here is an example that should work: 这是一个应该起作用的示例:

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;
}

You can probably figure out what the problem is by comparing the SVG text to what you have. 您可以通过将SVG文本与您所拥有的内容进行比较来找出问题所在。 If you can't you'll need to post your SVG file somewhere to allow other people to see it. 如果不能,则需要将SVG文件发布到其他人可以看到的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM