dqmhgz5848 2012-02-10 10:24
浏览 107
已采纳

如何使用PHP Imagick读取给定大小的SVG?

I have the following code:

$image = new Imagick();
$image->setBackgroundColor(new ImagickPixel('green'));
$image->setSize(20,20);
$image->readImageBlob(file_get_contents('./some/path/image.svg'));

It loads the SVG just fine but the setSize just gets completely ignored. It renders at 550x100, as per it's definition:

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
    width="550px" height="100px" viewBox="0 0 550 100" 
    enable-background="new 0 0 550 100" xml:space="preserve">

Has anyone got experience in getting SVG files to play nice with setSize?

  • 写回答

2条回答 默认 最新

  • doumu6997 2012-09-21 19:32
    关注

    The setSize() function is only for raw image formats, not SVG. You need to use scaleImage() instead...

    $image = new Imagick();
    $image->setBackgroundColor(new ImagickPixel('green'));
    $image->readImage('./some/path/image.svg');
    $image->scaleImage(20,20);
    

    By the way, that will work fine if you're downscaling your SVG image to a smaller size (as you are in this case), for for anyone who is upscaling it instead, you need to do one other thing. If you just change the size as above, it will appear jagged and pixelated. In that case you need to use setResolution() to increase the resolution like this:

    $image = new Imagick();
    $image->setResolution(2000,2000);
    $image->setBackgroundColor(new ImagickPixel('green'));
    $image->readImage('./some/path/image.svg');
    $image->scaleImage(1000,1000);
    

    The actual values for setResolution() should be computed as 72 * (final_size / original_size) (well, I think that's the correct formula anyway). But any value that's at least that value or higher will also work fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog