Certainly! Below is a simple but functional example of a webpage that allows a user to:
- Upload an SVG file (vector graphic).
- Choose the output resolution (width and height in pixels).
- Choose a scaling factor.
- Convert the SVG to a PNG image.
- Download the resulting PNG.
How it works:
- The user uploads an SVG file.
- The SVG is loaded into an
<img> element.
- A canvas is used to draw the SVG at the chosen resolution and scale.
- The canvas content is converted to a PNG data URL.
- The user can download the PNG.
Complete HTML + JavaScript code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SVG to PNG Converter</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 2rem;
max-width: 600px;
}
label {
display: block;
margin-top: 1rem;
}
input[type="number"] {
width: 80px;
}
#preview {
margin-top: 1rem;
border: 1px solid #ccc;
max-width: 100%;
}
#downloadLink {
display: inline-block;
margin-top: 1rem;
padding: 0.5rem 1rem;
background: #007bff;
color: white;
text-decoration: none;
border-radius: 4px;
}
#downloadLink:disabled {
background: #aaa;
}
</style>
</head>
<body>
<h1>SVG to PNG Converter</h1>
<label>
Upload SVG file:
<input type="file" id="svgFileInput" accept=".svg" />
</label>
<label>
Output Width (px):
<input type="number" id="widthInput" min="1" value="500" />
</label>
<label>
Output Height (px):
<input type="number" id="heightInput" min="1" value="500" />
</label>
<label>
Scale (e.g. 1 = 100%):
<input type="number" id="scaleInput" min="0.1" step="0.1" value="1" />
</label>
<button id="convertBtn" disabled>Convert to PNG</button>
<canvas id="canvas" style="display:none;"></canvas>
<div>
<img id="preview" alt="PNG Preview" />
</div>
<a id="downloadLink" href="#" download="converted.png" style="display:none;">Download PNG</a>
<script>
const svgFileInput = document.getElementById('svgFileInput');
const widthInput = document.getElementById('widthInput');
const heightInput = document.getElementById('heightInput');
const scaleInput = document.getElementById('scaleInput');
const convertBtn = document.getElementById('convertBtn');
const canvas = document.getElementById('canvas');
const preview = document.getElementById('preview');
const downloadLink = document.getElementById('downloadLink');
let svgText = null;
svgFileInput.addEventListener('change', () => {
const file = svgFileInput.files[0];
if (!file) {
convertBtn.disabled = true;
return;
}
if (file.type !== 'image/svg+xml' && !file.name.endsWith('.svg')) {
alert('Please upload a valid SVG file.');
convertBtn.disabled = true;
return;
}
const reader = new FileReader();
reader.onload = e => {
svgText = e.target.result;
convertBtn.disabled = false;
preview.src = '';
downloadLink.style.display = 'none';
};
reader.readAsText(file);
});
convertBtn.addEventListener('click', () => {
if (!svgText) return;
const width = parseInt(widthInput.value, 10);
const height = parseInt(heightInput.value, 10);
const scale = parseFloat(scaleInput.value);
if (isNaN(width) || width <= 0 || isNaN(height) || height <= 0 || isNaN(scale) || scale <= 0) {
alert('Please enter valid positive numbers for width, height, and scale.');
return;
}
// Create a Blob URL for the SVG
const svgBlob = new Blob([svgText], { type: 'image/svg+xml' });
const url = URL.createObjectURL(svgBlob);
const img = new Image();
img.onload = () => {
// Set canvas size according to width, height, and scale
canvas.width = width * scale;
canvas.height = height * scale;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the SVG image onto the canvas, scaled
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
// Convert canvas to PNG data URL
const pngDataUrl = canvas.toDataURL('image/png');
// Show preview
preview.src = pngDataUrl;
// Setup download link
downloadLink.href = pngDataUrl;
downloadLink.style.display = 'inline-block';
// Release object URL
URL.revokeObjectURL(url);
};
img.onerror = () => {
alert('Failed to load SVG image.');
URL.revokeObjectURL(url);
};
img.src = url;
});
</script>
</body>
</html>
How to use:
- Open this HTML file in a modern browser.
- Upload your SVG file.
- Adjust the width, height, and scale as desired.
- Click Convert to PNG.
- Preview the PNG below.
- Click Download PNG to save the image.
If you want me to add more features (like DPI control, background color, or multiple export formats), just ask!