0

I want to convert SVG to JPG, according to Convert SVG image to PNG with PHP I've created conversion method

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?

Community
  • 1
  • 1
Michal
  • 1,955
  • 5
  • 33
  • 56

1 Answers1

0

It's very likely that you're trying to read something that ImageMagick doesn't consider to be valid 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. If you can't you'll need to post your SVG file somewhere to allow other people to see it.

Danack
  • 24,939
  • 16
  • 90
  • 122