-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,234 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0.18 | ||
0.0.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare class SvgToPngConverter { | ||
convert(svgContent: string, outputPath: string, width?: number, height?: number): Promise<void>; | ||
} | ||
export default SvgToPngConverter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"use strict"; | ||
// class/SvgToPngConverter.ts | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// Copyright 2023 Scape Agency BV | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ============================================================================ | ||
// Import | ||
// ============================================================================ | ||
var sharp_1 = __importDefault(require("sharp")); | ||
var jsdom_1 = require("jsdom"); | ||
var fs_1 = __importDefault(require("fs")); | ||
var path_1 = __importDefault(require("path")); | ||
// ============================================================================ | ||
// Classes | ||
// ============================================================================ | ||
class SvgToPngConverter { | ||
async convert(svgContent, outputPath, width, height) { | ||
try { | ||
// Ensure the output directory exists | ||
const outputDir = path_1.default.dirname(outputPath); | ||
if (!fs_1.default.existsSync(outputDir)) { | ||
fs_1.default.mkdirSync(outputDir, { recursive: true }); | ||
} | ||
// Create a JSDOM instance to parse the SVG | ||
const dom = new jsdom_1.JSDOM(svgContent); | ||
const svgElement = dom.window.document.querySelector('svg'); | ||
if (!svgElement) { | ||
throw new Error('Invalid SVG content'); | ||
} | ||
if (width) { | ||
svgElement.setAttribute('width', width.toString()); | ||
} | ||
if (height) { | ||
svgElement.setAttribute('height', height.toString()); | ||
} | ||
const updatedSvgContent = svgElement.outerHTML; | ||
const pngBuffer = await (0, sharp_1.default)(Buffer.from(updatedSvgContent)).png().toBuffer(); | ||
await (0, sharp_1.default)(pngBuffer).toFile(outputPath); | ||
} | ||
catch (error) { | ||
console.error(`Error converting SVG to PNG: ${error}`); | ||
throw error; | ||
} | ||
} | ||
} | ||
// ============================================================================ | ||
// Export | ||
// ============================================================================ | ||
exports.default = SvgToPngConverter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// class/SvgToPngConverter.ts | ||
|
||
// Copyright 2023 Scape Agency BV | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
|
||
// ============================================================================ | ||
// Import | ||
// ============================================================================ | ||
|
||
import sharp from 'sharp'; | ||
import { JSDOM } from 'jsdom'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
|
||
// ============================================================================ | ||
// Classes | ||
// ============================================================================ | ||
|
||
class SvgToPngConverter { | ||
async convert(svgContent: string, outputPath: string, width?: number, height?: number): Promise<void> { | ||
try { | ||
|
||
// Ensure the output directory exists | ||
const outputDir = path.dirname(outputPath); | ||
if (!fs.existsSync(outputDir)) { | ||
fs.mkdirSync(outputDir, { recursive: true }); | ||
} | ||
|
||
// Create a JSDOM instance to parse the SVG | ||
const dom = new JSDOM(svgContent); | ||
const svgElement = dom.window.document.querySelector('svg'); | ||
|
||
if (!svgElement) { | ||
throw new Error('Invalid SVG content'); | ||
} | ||
|
||
if (width) { | ||
svgElement.setAttribute('width', width.toString()); | ||
} | ||
|
||
if (height) { | ||
svgElement.setAttribute('height', height.toString()); | ||
} | ||
|
||
const updatedSvgContent = svgElement.outerHTML; | ||
const pngBuffer = await sharp(Buffer.from(updatedSvgContent)).png().toBuffer(); | ||
|
||
await sharp(pngBuffer).toFile(outputPath); | ||
} catch (error) { | ||
console.error(`Error converting SVG to PNG: ${error}`); | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
|
||
// ============================================================================ | ||
// Export | ||
// ============================================================================ | ||
|
||
export default SvgToPngConverter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.