![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
svg2img-lock-canvas
Advanced tools
A high-performance in-memory convertor to convert svg to png/jpeg images for Node.
A high-performance in-memory convertor to convert svg to png/jpeg images for Node. Based on canvg and node-canvas
Because of jsdom, it requires Node.js 4 or newer.
Please notice: this library is only for Node, can not run in browsers.
npm install svg2img
To output JPEG image, you should install node-canvas
with jpeg support.
var fs = require('fs');
var svg2img = require('svg2img');
var btoa = require('btoa');
var svgString = [
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="236" height="120" ',
'viewBox="0 0 236 120">',
'<rect x="14" y="23" width="200" height="50" fill="#55FF55" stroke="black" stroke-width="1" />',
'</svg>'
].join('');
//1. convert from svg string
svg2img(svgString, function(error, buffer) {
//returns a Buffer
fs.writeFileSync('foo1.png', buffer);
});
//2. convert from svg's base64 string
svg2img(
'data:image/svg+xml;base64,'+ btoa(svgString),
function(error, buffer) {
fs.writeFileSync('foo2.png', buffer);
});
fs.writeFileSync('foo.svg', new Buffer(svgString));
//3. convert from a local file
svg2img(__dirname+'/foo.svg', function(error, buffer) {
fs.writeFileSync('foo3.png', buffer);
});
//4. convert from a remote file
svg2img(
'https://upload.wikimedia.org/wikipedia/commons/a/a0/Svg_example1.svg',
function(error, buffer) {
fs.writeFileSync('foo4.png', buffer);
});
//5. convert to jpeg file
svg2img(svgString, {format:'jpg','quality':75}, function(error, buffer) {
//default jpeg quality is 75
fs.writeFileSync('foo5.jpg', buffer);
});
You can scale the svg by giving width and height.
svg2img(__dirname+'/foo.svg', {'width':800, 'height':600} ,function(error, buffer) {
fs.writeFileSync('foo.png', buffer);
});
By default, the aspect ratio isn't preserved when scaling (same as preserveAspectRatio="none"
). You can change it using preserveAspectRatio
option. It can be true
to keep original value or any other value to replace with.
// use original preserveAspectRatio
svg2img(
__dirname+'/foo.svg',
{width:800, height:600, preserveAspectRatio:true},
function(error, buffer) {
fs.writeFileSync('foo.png', buffer);
}
);
// use custom preserveAspectRatio
svg2img(
__dirname+'/foo.svg',
{width:800, height:600, preserveAspectRatio:'xMinYMid meet'},
function(error, buffer) {
fs.writeFileSync('foo.png', buffer);
}
);
npm test
FAQs
A high-performance in-memory convertor to convert svg to png/jpeg images for Node.
We found that svg2img-lock-canvas demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.