如何使用PHP Imagick读取给定大小的SVG? [英] How to read an SVG with a given size using PHP Imagick?

查看:167
本文介绍了如何使用PHP Imagick读取给定大小的SVG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

它很好地加载了SVG,但是setSize却被完全忽略了.按照其定义,它以550x100的分辨率渲染:

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">

有人有没有获得过SVG文件以便与setSize配合使用的经验?

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

推荐答案

setSize()函数仅适用于原始图像格式,不适用于SVG.您需要改用scaleImage()...

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

顺便说一句,如果您将SVG图像缩小到较小尺寸(如本例所示),则可以很好地工作,对于任何将其放大的人,您都需要做另一件事.如果仅按上述方法更改大小,它将显示锯齿状和像素化.在这种情况下,您需要使用setResolution()来提高分辨率,如下所示:

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

setResolution()的实际值应计算为72 *(final_size/original_size)(好吧,我仍然认为这是正确的公式).但是任何至少等于或大于该值的值也可以正常工作.

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.

这篇关于如何使用PHP Imagick读取给定大小的SVG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆