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

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

问题描述

我已经在Google上进行了尽可能多的搜索,但是我只找到了一个调用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(您可以使用

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天全站免登陆