npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

rzl-cmd-svg-to-png-converter

v1.0.0

Published

A Node.js script to convert SVG files to PNG format

Readme

SVG转PNG转换器

一个基于Node.js的SVG到PNG格式转换工具,支持单文件转换和批量转换。

功能特性

  • ✅ 单个SVG文件转换为PNG
  • ✅ 批量转换目录中的所有SVG文件
  • ✅ 递归处理子目录(保持目录结构)
  • ✅ 自定义输出尺寸(宽度、高度)
  • ✅ 可调节DPI密度
  • ✅ 透明背景支持
  • ✅ 命令行界面,使用简单
  • ✅ 详细的转换进度和结果反馈
  • ✅ 智能进度显示

系统要求

  • Node.js 14.0.0 或更高版本
  • npm 或 yarn 包管理器

安装

  1. 克隆或下载项目文件
  2. 在项目目录中安装依赖:
npm install

使用方法

基本用法

# 显示帮助信息
node svg-to-png.js --help

# 转换单个文件
node svg-to-png.js input.svg output.png

# 转换单个文件(自动生成输出文件名)
node svg-to-png.js input.svg

# 批量转换目录中的所有SVG文件
node svg-to-png.js ./svg-files ./png-files

高级选项

# 设置输出尺寸
node svg-to-png.js input.svg output.png --width 800 --height 600

# 设置DPI密度(提高清晰度)
node svg-to-png.js input.svg output.png --density 150

# 批量转换并设置参数
node svg-to-png.js ./svg-files ./png-files --width 1024 --density 200

参数说明

| 参数 | 说明 | 默认值 | |------|------|--------| | --width | 输出PNG的宽度(像素) | 保持原始比例 | | --height | 输出PNG的高度(像素) | 保持原始比例 | | --density | DPI密度,影响图像清晰度 | 72 | | --recursive | 递归处理子目录中的SVG文件 | false | | --help | 显示帮助信息 | - |

使用示例

示例1:转换单个文件

node svg-to-png.js example.svg

这将在同一目录下生成 example.png 文件。

示例2:指定输出路径和尺寸

node svg-to-png.js example.svg ./output/logo.png --width 512 --height 512

示例3:批量转换

# 假设有一个包含多个SVG文件的目录
node svg-to-png.js ./icons ./png-icons --density 150

这将把 ./icons 目录中的所有SVG文件转换为PNG格式,并保存到 ./png-icons 目录中。

示例4:递归批量转换

# 递归处理包含子目录的项目
node svg-to-png.js ./project-assets ./output-assets --recursive --width 512

这将递归处理 ./project-assets 目录及其所有子目录中的SVG文件,保持原有的目录结构,并将所有图片设置为512像素宽度。

示例5:复杂项目结构转换

# 处理复杂的项目图标结构
node svg-to-png.js ./src/assets/icons ./dist/icons --recursive --density 200

适用于处理如下结构的项目:

src/assets/icons/
├── common/
│   ├── home.svg
│   └── settings.svg
├── social/
│   ├── facebook.svg
│   └── twitter.svg
└── navigation/
    ├── arrow-left.svg
    └── arrow-right.svg

项目结构

svg-to-png-converter/
├── package.json          # 项目配置和依赖
├── svg-to-png.js         # 主要转换脚本
├── example.svg           # 示例SVG文件
└── README.md             # 说明文档

技术实现

本工具使用以下技术:

  • Node.js: 运行环境
  • Sharp: 高性能图像处理库,支持SVG到PNG的转换
  • 文件系统操作: 处理文件和目录的读写

注意事项

  1. 依赖安装: 首次使用前必须运行 npm install 安装依赖
  2. 文件格式: 输入文件必须是有效的SVG格式
  3. 输出目录: 如果输出目录不存在,程序会自动创建
  4. 尺寸设置: 如果只设置宽度或高度,程序会保持原始宽高比
  5. 透明背景: 默认输出PNG具有透明背景
  6. 错误处理: 程序会显示详细的错误信息,帮助诊断问题

常见问题

Q: 安装依赖时出现错误怎么办?

A: 确保你的Node.js版本在14.0.0以上,并尝试清除npm缓存:

npm cache clean --force
npm install

Q: 转换后的PNG文件很大怎么办?

A: 可以适当降低DPI密度或减小输出尺寸:

node svg-to-png.js input.svg output.png --density 72 --width 400

Q: 批量转换时部分文件失败怎么办?

A: 程序会显示详细的错误信息,通常是因为SVG文件格式不正确或包含不支持的元素。

许可证

MIT License

贡献

欢迎提交Issue和Pull Request来改进这个工具!