在 PHP 中将 SVG 文件渲染为 PNG 或 JPEG [英] Rendering an SVG file to a PNG or JPEG in PHP

查看:29
本文介绍了在 PHP 中将 SVG 文件渲染为 PNG 或 JPEG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尽可能多地搜索了谷歌,但我只找到了一个调用 Inkscape 来渲染 SVG 的 PHP 类,所以我在这里问:

I've googled as much as I can, but I've only found a PHP class that calls upon Inkscape to render the SVG, so I ask here:

我有一个以某种方式生成(或由客户端上传)的有效 SVG 文件.我需要仅使用 PHP 和/或 GDLib 将其呈现为 JPG 或 PNG,因为并非所有浏览器都支持 SVG.

I have a valid SVG file generated in some way (or uploaded by a client). I need to render this into a JPG or PNG using just PHP and/or GDLib, as SVG is not supported by all browsers.

我没有安装任何东西的选项,因此使用 GDLib 将 SVG 转换为 PNG 的类将是最理想的.

I do not have the option of installing anything, so a class that converts SVG to PNG using GDLib would be the most ideal.

推荐答案

检查 ImageMagick 是否安装(可以使用 phpinfo).如果是,您可以使用以下代码覆盖到 PNG.

Check if ImageMagick is installed (you can find out using phpinfo). If it is, you can use the following code to cover to a PNG.

$image = new Imagick();
$image->readImageBlob(file_get_contents('image.svg'));
$image->setImageFormat("png24");
$image->resizeImage(1024, 768, imagick::FILTER_LANCZOS, 1); 
$image->writeImage('image.png');

有很多讨论这个的线程.特别有用的是这个线程:使用 PHP 将 SVG 图像转换为 PNG

There are many threads that discuss this. One that is particularly useful is this thread: Convert SVG image to PNG with PHP

这篇关于在 PHP 中将 SVG 文件渲染为 PNG 或 JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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