how to resize an SVG with Imagick/ImageMagick

后端 未结 4 705

I am sending a string representation of an SVG file to the server and using Imagick to turn this into a jpeg in the following manner:

$image = stripslashes($         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-29 10:36

    I was looking for a solution, and i found this just after reading this post, and work like a charm:

    $im = new Imagick();
    $im->readImage("/path/to/image.svg");
    $res = $im->getImageResolution();
    $x_ratio = $res['x'] / $im->getImageWidth();
    $y_ratio = $res['y'] / $im->getImageHeight();
    $im->removeImage();
    $im->setResolution($width_in_pixels * $x_ratio, $height_in_pixels * $y_ratio);
    $im->readImage("/path/to/image.svg");
    // Now you can do anything with the image, such as convert to a raster image and output it to the browser:
    $im->setImageFormat("png");
    header("Content-Type: image/png");
    echo $im;
    

    Credits go to the author of that comment in php manuals page.

提交回复
热议问题