如何在不损失质量的情况下用PHP扩展SVG并另存为PNG? [英] How can I scale up an SVG and save as PNG, with PHP without losing quality?

查看:156
本文介绍了如何在不损失质量的情况下用PHP扩展SVG并另存为PNG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一批SVG图像,需要调整其大小然后保存为PNG,我想使用PHP来做到这一点.我也曾问过类似的问题,但没有一个解决方案对我有用.例如:

I have a batch of SVG images that I need to resize and then save as PNG, and I'd like to use PHP to do it. Similar questions to mine have been asked but none of the solutions work for me. For example:

选项A :使用PHP更改SVG的width和height属性.

option A: use PHP to change the width and height attribute of the SVG.

A的问题:这似乎很好,但实际上它只会增加画布的大小,而不会增加画布的内容.我不确定这是否重要,但是我的SVG是用Illustrator创建的小型图形,主要由路径元素组成.

problem with A: this seems to work fine but in fact it only increases the canvas size, not the contents of the canvas. I'm not sure if it's important but my SVGs are small graphics that were created with Illustrator, mostly made up of path elements.

选项B :使用imagick调整大小并重新保存

option B: use imagick to resize and resave

示例:

$im = new Imagick();
$im->setResolution( $startResolution * ($newWidth / $oldWidth ), $startResolution * ($newHeight / $oldHeight ) );
$im->readImage( $svgSource );
$im->setImageFormat("png32");
echo '<img src="data:image/png;base64,' . base64_encode($im) . '"  />';

B的问题:这似乎也可以很好地工作,但是生成的png会变得模糊

problem with B: this also seems to work fine but the resulting png will be blurry

选项C :选项B的变体,您将分辨率设置得很高,例如5000,然后调整大小.

option C: variant of option B, you set the resolution really high, like 5000, then resize.

C的问题:生成的PNG仍然模糊

problem with C: the resulting PNG is still blurry

选项D :使用exec(convert ...)解决PHP的错误

option D: use exec(convert...) to get around PHP's bugs

D问题:您猜对了,生成的PNG仍然模糊

problem with D: you guessed it, the resulting PNG is still blurry

有人找到使这项工作成功的方法吗?

Has anyone found a way to make this work?

推荐答案

对于选项A:

如果svg期望坐标系与创建时的坐标系相同(请参阅:原始宽度/高度),则需要添加viewBox属性(使用0 0 originalwidth originalheight).然后,当您设置新的width和height属性时,svg应该适当地将其自身调整为新的大小.

If the svg expects the coordinate system to be whatever it was when it was created (read: the original width/height), then you will need to add a viewBox attribute (use 0 0 originalwidth originalheight). Then when you set new width and height attributes the svg should rescale itself properly to the new size.

这篇关于如何在不损失质量的情况下用PHP扩展SVG并另存为PNG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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