php - How to change dimensions of SVG and print it as PNG?
问题描述
I have an svg file, which contains a small vector. It looks like this:
I have the following code which converts SVG to PNG:
$imagick = new Imagick();
$imagick->setBackgroundColor(new ImagickPixel("transparent"));
$imagick->readImageBlob(file_get_contents($_GET['src']));
$imagick->setImageFormat('png32');
// Print
header("Content-Type: image/png");
echo $imagick;
$imagick->clear();
$imagick->destroy();
return;
Which is accessible using: http://localhost/imagick.php?src=Untitled-1.svg
And I want to resize the SVG and keep the quality, like Wikipedia does, for example:
http://localhost/imagick.php?src=Untitled-1.svg&dimensions=[250]
Or http://localhost/imagick.php?src=Untitled-1.svg&dimensions=[250,250]
and then to print it with the new dimensions:
if (isset($_GET['dimensions'])) {
$_GET['dimensions'] = json_decode($_GET['dimensions']);
$imagick->resizeImage(
(isset($_GET['dimensions'][0]) ? $_GET['dimensions'][0] : 0),
(isset($_GET['dimensions'][1]) ? $_GET['dimensions'][1] : 0),
Imagick::FILTER_UNDEFINED,
0.5
);
}
But when I do it, The quality is REALLY BAD:
How can I preserve the original quality so it will be something like:
?
Full Code:
$imagick = new Imagick();
$imagick->setBackgroundColor(new ImagickPixel("transparent"));
$imagick->readImageBlob(file_get_contents($_GET['src']));
if (isset($_GET['dimensions'])) {
$_GET['dimensions'] = json_decode($_GET['dimensions']);
$imagick->resizeImage(
(isset($_GET['dimensions'][0]) ? $_GET['dimensions'][0] : 0),
(isset($_GET['dimensions'][1]) ? $_GET['dimensions'][1] : 0),
Imagick::FILTER_UNDEFINED,
0.5
);
}
$imagick->setImageFormat('png32');
// Print
header("Content-Type: image/png");
echo $imagick;
$imagick->clear();
$imagick->destroy();
return;
解决方案
推荐阅读
- python - 在 Python 中引发异常类
- javascript - 未处理的承诺拒绝:错误:地图容器已初始化
- python - 如何使用 SQL Alchemy 在 SQL Server 中创建图形节点表?
- python - 使用 BeautifulSoup 附加表,使其工作?
- flutter - 数量增加减少显示计数器字段
- javascript - 如何使用 Vuetify.js 的 v-simple-table 将标题放在第一列?
- python - 无法从 Visual Studio 代码解释器更改 python 版本
- ios12 - ios 12 浏览器中的 Samesite 问题
- kotlin - 如何在 Kotlin 中跳过泛型类型参数的规范?
- c++ - 为什么我会发出叮当声:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)