├── public └── favicon.ico ├── .eslintrc.json ├── next.config.js ├── next-env.d.ts ├── pages ├── _app.tsx └── index.tsx ├── package.json ├── .gitignore ├── tsconfig.json ├── LICENSE ├── styles └── app.module.css └── README.md /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xitanggg/svg-to-png-jpeg-favicon/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals", 3 | "rules": { 4 | "react/no-unescaped-entities": "off" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | reactStrictMode: true, 4 | } 5 | 6 | module.exports = nextConfig 7 | -------------------------------------------------------------------------------- /next-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | 4 | // NOTE: This file should not be edited 5 | // see https://nextjs.org/docs/basic-features/typescript for more information. 6 | -------------------------------------------------------------------------------- /pages/_app.tsx: -------------------------------------------------------------------------------- 1 | import 'antd/dist/antd.css'; 2 | import type { AppProps } from 'next/app'; 3 | 4 | function MyApp({ Component, pageProps }: AppProps) { 5 | return ; 6 | } 7 | 8 | export default MyApp; 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svg-to-png-jpeg-favicon", 3 | "private": true, 4 | "scripts": { 5 | "dev": "next dev", 6 | "build": "next build", 7 | "start": "next start", 8 | "lint": "next lint" 9 | }, 10 | "dependencies": { 11 | "antd": "^4.18.3", 12 | "next": "12.0.8", 13 | "react": "17.0.2", 14 | "react-dom": "17.0.2", 15 | "react-github-btn": "^1.2.1" 16 | }, 17 | "devDependencies": { 18 | "@types/node": "17.0.8", 19 | "@types/react": "17.0.38", 20 | "eslint": "8.6.0", 21 | "eslint-config-next": "12.0.8", 22 | "typescript": "4.5.4" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env.local 29 | .env.development.local 30 | .env.test.local 31 | .env.production.local 32 | 33 | # vercel 34 | .vercel 35 | 36 | # typescript 37 | *.tsbuildinfo 38 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true 17 | }, 18 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], 19 | "exclude": ["node_modules"] 20 | } 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Xitang Zhao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /styles/app.module.css: -------------------------------------------------------------------------------- 1 | .main { 2 | min-width: 400px; 3 | padding: 20px 0px; 4 | display: flex; 5 | flex-direction: column; 6 | align-items: center; 7 | } 8 | 9 | .spanText { 10 | padding: 2px 3px; 11 | background-color: #fafafa; 12 | border: 1px solid #d9d9d9; 13 | } 14 | 15 | .dragger { 16 | padding: 0px 20px; 17 | width: 400px !important; 18 | height: 200px !important; 19 | } 20 | 21 | .dragger > span { 22 | display: flex !important; 23 | flex-direction: column; 24 | align-items: center; 25 | } 26 | 27 | .canvasWrapper { 28 | height: 110px; 29 | display: flex; 30 | justify-content: center; 31 | align-items: center; 32 | } 33 | 34 | .canvas { 35 | margin-top: 20px; 36 | max-width: 100px; 37 | } 38 | 39 | .inputGroup { 40 | margin: 30px 0px; 41 | width: 400px; 42 | display: flex; 43 | } 44 | 45 | .inputGroup input { 46 | text-align: center !important; 47 | } 48 | 49 | .inputGroup > div:first-of-type { 50 | margin-right: 10px; 51 | } 52 | 53 | .radioLabel { 54 | background-color: #fafafa; 55 | cursor: text; 56 | } 57 | 58 | .radioLabel:hover { 59 | color: initial; 60 | } 61 | 62 | .radioGroup { 63 | margin-bottom: 30px; 64 | } 65 | 66 | .footer { 67 | display: flex; 68 | flex-direction: column; 69 | align-items: center; 70 | padding-bottom: 5px; 71 | } 72 | 73 | .footer > div { 74 | margin-bottom: 15px; 75 | } 76 | 77 | .footer > div:nth-of-type(2) { 78 | margin-bottom: 5px; 79 | } 80 | 81 | .footer > div > span:first-of-type { 82 | margin-right: 5px; 83 | } 84 | 85 | .collapse { 86 | max-width: 400px; 87 | width: 100%; 88 | } 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🎨 SVG to PNG / JPEG / ICO (Favicon) Converter 2 | 3 | Hi there👋 This is a free online tool that allows you to convert `SVG` files to `PNG`, `JPEG`, `ICO` (Favicon) images. 4 | 5 | It is very simple and fast - it runs only in your browser and allows you to customize image width and height💨 Try it out here: [https://svg-to-png-jpeg-favicon.vercel.app](https://svg-to-png-jpeg-favicon.vercel.app) 6 | 7 | ![Gif Demo](https://media.giphy.com/media/QFgU6UbCo7rv4idP90/giphy.gif) 8 | 9 | ## 💡 Story 10 | 11 | While trying to do some svg to images conversion, I couldn't find any simple online tool that allows me to do so🤯 This was frustrating since the latest `Web APIs` - [URL API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) and [Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) have made it very easy to convert files to different image types on a modern browser. So I went ahead and coded out this simple tool myself and shared it out there with others who might also experience the frustration🔥 12 | 13 | ## ⚙️ Implementation 14 | 15 | This tool is open-source - you can use it for anything and read about its full implementation in `/pages/index.tsx`. The core utility is the `Canvas Class`, which is only ~100 lines of JavaScript code. At high level, it uses the below [Web APIs](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Introduction) to perform the svg conversion: 16 | 17 | 1. [URL.createObjectURL](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL) to read the svg file and convert it to a DOMString 18 | 2. [CanvasRenderingContext2D.drawImage](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage) to render a svg image on a canvas without losing resolution 19 | 3. [HTMLCanvasElement.toDataURL](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL) to convert the svg file on canvas to be different image types based on user selection - `.png` `.jpeg` `.ico` 20 | 21 | Since Web APIs are bundled into a browser, you can run this tool entirely offline on any modern day browser. 22 | 23 | ## 🍔 Tech Stack 24 | 25 | | Language | [TypeScript](https://www.typescriptlang.org/) | 26 | | -------------------- | --------------------------------------------- | 27 | | JavaScript Framework | React & [Next.js](https://nextjs.org) | 28 | | React UI Framework | [Ant Design](https://ant.design) | 29 | | CSS | CSS modules | 30 | | Deployment | [Vercel](https://vercel.com)\* | 31 | 32 | \*Vercel is used over GitHub page to deploy this tool because it has better SEO support with SSG and can be potentially helpful for others to find it on Google 33 | 34 | ## 💻Local Testing 35 | 36 | ```bash 37 | git clone https://github.com/xitanggg/svg-to-png-jpeg-favicon.git 38 | cd ./svg-to-png-jpeg-favicon 39 | npm install 40 | npm run dev 41 | ``` 42 | 43 | Finally, open [http://localhost:3000](http://localhost:3000) with your browser to use the tool locally 🚀 44 | -------------------------------------------------------------------------------- /pages/index.tsx: -------------------------------------------------------------------------------- 1 | import type { NextPage } from 'next'; 2 | import Head from 'next/head'; 3 | import React, { useEffect, useRef, useState } from 'react'; 4 | import styles from '../styles/app.module.css'; 5 | import { 6 | Button, 7 | Upload, 8 | InputNumber, 9 | Radio, 10 | RadioChangeEvent, 11 | Divider, 12 | Collapse, 13 | } from 'antd'; 14 | const { Dragger } = Upload; 15 | import type { UploadChangeParam } from 'antd/lib/upload'; 16 | import type { UploadFile } from 'antd/lib/upload/interface'; 17 | const { Panel } = Collapse; 18 | import dynamic from 'next/dynamic'; 19 | const GitHubButtonWithNoSSR = dynamic(() => import('react-github-btn'), { 20 | ssr: false, 21 | }); 22 | 23 | enum FileType { 24 | png = 'png', 25 | jpeg = 'jpeg', 26 | ico = 'ico', 27 | } 28 | 29 | // Canvas Class with helpful utils to add, update, and convert svg to different image type 30 | class Canvas { 31 | canvas: HTMLCanvasElement; 32 | ctx: CanvasRenderingContext2D; 33 | svgName: string; 34 | svgURL: string; 35 | downloadType: FileType; 36 | 37 | constructor( 38 | canvas: HTMLCanvasElement, 39 | width: number, 40 | height: number, 41 | type: FileType 42 | ) { 43 | this.canvas = canvas; 44 | this.canvas.width = width; 45 | this.canvas.height = height; 46 | this.ctx = canvas.getContext('2d') as CanvasRenderingContext2D; 47 | this.svgName = ''; 48 | this.svgURL = ''; 49 | this.downloadType = type; 50 | } 51 | 52 | // Clear canvas as transparent (white for .jpeg) 53 | clear() { 54 | const { width, height } = this.canvas; 55 | if (this.downloadType === FileType.jpeg) { 56 | this.ctx.fillStyle = 'white'; 57 | this.ctx.fillRect(0, 0, width, height); 58 | } else { 59 | this.ctx.clearRect(0, 0, width, height); 60 | } 61 | } 62 | 63 | // Draw svg file on canvas 64 | drawSvg() { 65 | const img = new Image(); 66 | const { width, height } = this.canvas; 67 | img.onload = () => { 68 | this.ctx.drawImage(img, 0, 0, width, height); 69 | }; 70 | img.src = this.svgURL; 71 | } 72 | 73 | // Add svg file on canvas and draw it 74 | addSvg(svgName: string = 'file', svgFile: Blob | any) { 75 | this.svgName = svgName; 76 | this.clear(); 77 | // Revoke previous svg object URL to prevent memory leak 78 | if (this.svgURL) URL.revokeObjectURL(this.svgURL); 79 | this.svgURL = URL.createObjectURL(svgFile); 80 | this.drawSvg(); 81 | } 82 | 83 | // Update svg file on canvas based on new width and height 84 | updateCanvas(width: number, height: number) { 85 | this.canvas.width = width; 86 | this.canvas.height = height; 87 | this.clear(); 88 | this.drawSvg(); 89 | } 90 | 91 | // Update download file type and svg file on canvas 92 | updateDownloadType(type: FileType) { 93 | this.downloadType = type; 94 | this.updateCanvas(this.canvas.width, this.canvas.height); 95 | } 96 | 97 | // Download svg file as image to the selected file type 98 | downloadCanvasAsImg(type: FileType) { 99 | let fileType, fileName; 100 | switch (type) { 101 | case FileType.ico: 102 | fileType = 'image/x-icon'; 103 | fileName = 'favicon.ico'; 104 | break; 105 | default: 106 | fileType = `image/${type}`; 107 | const { width, height } = this.canvas; 108 | if (width === height) fileName = `${this.svgName}-${width}.${type}`; 109 | else fileName = `${this.svgName}-${width}x${height}.${type}`; 110 | } 111 | const link = document.createElement('a'); 112 | link.download = fileName; 113 | link.href = this.canvas.toDataURL(fileType, 1.0); 114 | link.click(); 115 | } 116 | } 117 | 118 | let canvas: Canvas; 119 | const initialWidth: number = 48; 120 | const initialHeight: number = 48; 121 | const initialType: FileType = FileType.png; 122 | const Home: NextPage = () => { 123 | const canvasRef = useRef(null); 124 | const [width, setWidth] = useState(initialWidth); 125 | const [height, setHeight] = useState(initialHeight); 126 | const [type, setType] = useState(initialType); 127 | const [dummy, setDummy] = useState(false); 128 | 129 | // Create Canvas Object once component finishes mounting 130 | useEffect(() => { 131 | if (canvasRef.current && !canvas) 132 | canvas = new Canvas(canvasRef.current, width, height, type); 133 | }, []); 134 | 135 | // Handler when a file is added 136 | const onFileAdd = (info: UploadChangeParam) => { 137 | const { status, originFileObj: svgFile } = info.file; 138 | if (status === 'done') { 139 | const svgName = svgFile?.name.slice(0, -4); 140 | canvas.addSvg(svgName, svgFile); 141 | // React Escape Hatch: force a react refresh with dummy 142 | setDummy(!dummy); 143 | } 144 | }; 145 | 146 | const onWidthChange = (value: any) => { 147 | if (Number.isFinite(value)) { 148 | const newWidth = value; 149 | const newHeight = value; 150 | canvas.updateCanvas(newWidth, newHeight); 151 | setWidth(newWidth); 152 | setHeight(newHeight); 153 | } 154 | }; 155 | 156 | const onHeightChange = (value: any) => { 157 | if (Number.isFinite(value)) { 158 | const newHeight = value; 159 | canvas.updateCanvas(width, newHeight); 160 | setHeight(newHeight); 161 | } 162 | }; 163 | 164 | const onTypeChange = (e: RadioChangeEvent) => { 165 | const newType = e.target.value; 166 | canvas.updateDownloadType(newType); 167 | setType(newType); 168 | if (newType === FileType.ico) { 169 | setWidth(48); 170 | setHeight(48); 171 | } 172 | }; 173 | 174 | return ( 175 |
176 | 177 | 178 | SVG to PNG / JPEG / ICO (Favicon) Online Converter - Free and Fast 179 | 180 | 184 | 185 | 186 | 187 |
188 |

🎨 SVG Converter

189 |

190 | Convert .svg to{' '} 191 | .png{' '} 192 | .jpeg{' '} 193 | .ico like a breeze 💨{' '} 194 |

195 | 200 | setTimeout(() => onSuccess('success'), 0)) as any 201 | } 202 | className={styles.dragger} 203 | maxCount={1} 204 | showUploadList={false} 205 | onChange={onFileAdd} 206 | > 207 |

📥 Click or Drag SVG File Here

208 |
209 | 210 |
211 |
212 |
213 | 220 | 227 |
228 |
229 | 234 | 237 | {Object.keys(FileType).map((type) => ( 238 | 239 | {type} 240 | 241 | ))} 242 | 243 |
244 | 251 |
252 | 253 | 254 | 💡 This tool only runs in the browser and can be ran entirely offline 255 | 256 | 362 |
363 | ); 364 | }; 365 | 366 | export default Home; 367 | --------------------------------------------------------------------------------